How to center cell values in vba?

akika

New Member
Joined
Aug 2, 2018
Messages
10
Office Version
  1. 2016
Hi,

I have a vba function module in access that output a query to an excel type (97-2003 Worksheet).

Ive tried to center the cell contents from B2 To D10 using:

With sh.Range("B2:D10")
.HorizontalAlignment = xlCenter
End With

but am getting runtime error 1004: Unable to set HorizontalAlignment property of the Range class.
Any idea?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
If you don't have a reference set to the Excel library, add:

Code:
Const xlCenter as long = -4108
 
Upvote 0
No, I mean:

Code:
Const xlCenter as long = -4108
With sh.Range("B2:D10")
.HorizontalAlignment = xlCenter
End With

If you don't have a reference set to Excel, the xlCenter constant has no meaning in your code.
 
Upvote 0
No, I mean:

Code:
Const xlCenter as long = -4108
With sh.Range("B2:D10")
.HorizontalAlignment = xlCenter
End With

If you don't have a reference set to Excel, the xlCenter constant has no meaning in your code.
how u got the -4108 ? is it readily available with other codes?
which reference to use to make the font as white color within this range?
 
Upvote 0
Try This

Sub test()

Dim sh As Worksheet
Set sh = ActiveSheet


With sh.Range("B2:D10")
.HorizontalAlignment = xlCenter
End With

End Sub
Hi,

I have a vba function module in access that output a query to an excel type (97-2003 Worksheet).

Ive tried to center the cell contents from B2 To D10 using:

With sh.Range("B2:D10")
.HorizontalAlignment = xlCenter
End With

but am getting runtime error 1004: Unable to set HorizontalAlignment property of the Range class.
Any idea?
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,558
Latest member
aivin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top