How to center cell values in vba?

akika

New Member
Joined
Aug 2, 2018
Messages
8
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

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,747
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
If you don't have a reference set to the Excel library, add:

Code:
Const xlCenter as long = -4108
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,747
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
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.
 

akika

New Member
Joined
Aug 2, 2018
Messages
8

ADVERTISEMENT

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?
 

anand3dinesh

Board Regular
Joined
Dec 19, 2019
Messages
88
Office Version
  1. 2016
Platform
  1. Windows
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?
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,747
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
how u got the -4108
Using the object browser in Excel.

You can set the font using an RGB value like: .Font.Color = RGB(255,255,255)
 

Forum statistics

Threads
1,147,560
Messages
5,741,825
Members
423,689
Latest member
Jords998

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
Top