Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Excel VerticalAlignment = xlCenter not working

Tags:

excel

vba

The below code selects the sheet but fails to align the cells to center:

wb.Sheets(1).Columns("A:L").Select
With Selection
    .VerticalAlignment = xlCenter
End With

wb.Sheets(1).Activate
wb.Sheets(1).Columns("A:L").Select
With Selection
  .VerticalAlignment = xlCenter
End With 

Selects the entire sheet but it's not changing the vertical alignment to center.

wb.Sheets(1).Columns("A:L").VerticalAlignment = xlCenter

Does nothing.

I don't want HorizontalAlignment :)

I found out the column has VerticalAlignment set to xlCenter but the Cells underneath the column do not have VerticalAlignment set to xlCenter.

like image 752
Bruno Avatar asked Sep 15 '11 14:09

Bruno


1 Answers

Don't Select and don't work with Selection without a reason. That's Recorder's stuff. It is longer to read, slower to execute, and prone to error.

wb.Sheets(1).Columns("A:L").VerticalAlignment = xlCenter is much better.

If you need to do several things with the same range, then use With

with wb.Sheets(1).Columns("A:L")
      .VerticalAlignment = xlCenter
      .somethingElse
End with
like image 62
iDevlop Avatar answered Oct 03 '22 14:10

iDevlop