Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align text to left and right in Excel?

Tags:

excel

vba

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.

like image 437
Kevin López Avatar asked Oct 29 '25 05:10

Kevin López


2 Answers

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.

like image 166
Jsleshem Avatar answered Oct 31 '25 01:10

Jsleshem


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.

like image 38
Zachary Summers Avatar answered Oct 31 '25 00:10

Zachary Summers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!