I have a workbook in which I need to align text (from cells) to the left and the to the right. I have this so far but I don't know how to proceed.
Sub M()
ActiveSheet.PageSetup.CenterHeader = ActiveSheet.Range("A1") & " " & 
ActiveSheet.Range("B1")
End Sub
I want to know in general how to override the alignment for text in each part of the header but in this instance, I need to have text aligned to the left in both the right and left headers.
To do this in Excel, select the section of cells needed to align. Then, type alt+H+A+L for left, alt+H+A+C for center, and alt+H+A+R for right.
However, based on your question it seems like you want to do this in VBA instead of Excel. If that is the case, do this instead:
Range(myRange).HorizontalAlignment = xlRight for right, and
Range(myRange).HorizontalAlignment = xlLeft for left, where myRange is the range of cells.
The HorizontalAlignment property of the Range should be what you're looking for. xlLeft or xlRight are values to align left or right.
Range.HorizontalAlignment = xlLeft 
or
Range.HorizontalAlignment = xlRight
E.G.
Sub M()
ActiveSheet.PageSetup.CenterHeader = ActiveSheet.Range("A1") & " " & 
ActiveSheet.Range("B1").HorizontalAlignment = xlRight
End Sub
would align the B1 cell to the right.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With