Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Set Horizontal Alignment in Excel for (Range/Cell)

Status
Not open for further replies.

Candiman421

Programmer
Dec 9, 2022
1
US
thread705-696696

I ran into Excel VBA issue for how to set Range.HorizontalAlignment property. It kept throwing an error, and I couldn't find an answer for it anywhere, so I'd like to post a solution I found that works in case anyone runs across this issue again.

Get your target Range however you get your Ranges, and the secret is to set the property twice, and in this order.

Code:
Set excelWorkbook = Application.ActiveWorkbook
Set excelSheet = excelWorkbook.Sheets("MySheetCodeName")

excelSheet.Range("RangeNameOrAddress").Style.HorizontalAlignment = xlLeft
excelSheet.Range("RangeNameOrAddress").HorizontalAlignment       = xlLeft



And remember, a Cell IS a Range.
For reference, here are some enumerations to use, the second set is redundant

```
xlLeft
-4131
xlCenter
-4108
xlRight
-4152

xlHAlignLeft
-4131
xlHAlignCenter
-4108
xlHAlignRight
-4152
```


Happy Coding!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top