Candiman421
Programmer
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.
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!
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!