I'm currently using the following code to automatically align numbers, formulas and text in a table row centrally except for the first column in the selection which aligns left if it's text and centrally if it's not.
Annoyingly, however, if I only select one row, the macro starts running for every cell above and beneath the selection. Is there a way to fix this?
Also, currently once the macro has finished running it ends on the first column of the selection. Is there a way to make it end with the selection I started with (i.e. if I've selected cells A1:D1, once it's done running the currently selected cell will be A1 but I'd like it to still be highlighting A1:D1).
Apologies if anything is unclear
Sub Test_align_left()
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Selection.Columns(1).Select
On Error Resume Next
With Selection
.SpecialCells(xlCellTypeConstants, xlTextValues).HorizontalAlignment = xlLeft
.SpecialCells(xlCellTypeFormulas, xlTextValues).HorizontalAlignment = xlLeft
End With
End Sub
This is an example table I start with:
I select the first two rows:
And it works perfectly, the first row contains a number in the first column so it aligns centrally, and the second row has text in the first column so it aligns to the left - so far so good:
But, if I run the macro on this row:
Suddenly all cells with text align to the left, regardless of whether I selected them or not: