Align text to left and right in Excel?

Viewed 54157

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.

5 Answers

I found you can use .api to center / left / right align.

  • Set the value of the HorizontalAlignment property of the cell to:
    • Center-aligned, we’d set HorizontalValue to -4108;
    • Right-aligned, we’d set HorizontalValue to -4152;
    • Left-aligned, we’d set HorizontalValue to -4131;

What it would look like:

import xlwings as xw
sht = xw.sheets.active
sht.range(f'$A1:$C5').api.HorizontalAlignment = -4131
Related