HorizontalAlignment Error in Excel 97

W

Werner

Hi,

I have a HorizontalAlignment line code that doesn't want to work. It
says : "Error '1004' : Unable to set the HorizontalAlignment property
of the Range class.". I am in Excel 97 and it is frustrating not being
able to do such a simple task with VBA. It shouldn't be difficult.
Cells are not even merged. Any ideas?

Werner


Code:
--------------------


Worksheets("Formulaire").Range("A6:AE11").Select
With Selection
.HorizontalAlignment = xlLeft
End With
 
Joined
Dec 9, 2022
Messages
1
Reaction score
0
I ran into a similar issue with Excel 365. The previous solution did not work for me. I found I had to set the alignment twice, and in this order:

Just get your range however it is you like to get your ranges, I've included some surrounding code for context.


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!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top