'Excel VBA error 1004 when starting macro with alignment code from another sheet
the following code gives me error 1004 ("Unable to set the VerticalAlignment property of the Range class.") when the macro is started from Sheet1:
With Sheet2.Cells(1,1)
.VerticalAlignment = xlCenter
End with
".Value" or ".FontColor" works good, only this alignment code breaks. If i start the macro from Sheet2, then no problems at all. The sheets are password protected, but anyway the macro runs fine when started from Sheet2.
How can i solve this?
Solution 1:[1]
You can use Activate to select the sheet before changing the alignment
Worksheets("Worksheet Name").Activate
Using "With Sheet2..." should handle the activation however I was getting the same error when assigning properties to a sheet that was not activated. WrapText and RowHeight worked but setting the VerticalAlignment threw the 1004 error.
When I activated the sheet prior to changing the VerticalAlignment worked perfectly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zack Connaughton |
