在Excel中左右对齐文本?

时间:2022-08-22 12:16:13

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.

我想知道如何覆盖标题的每个部分中的文本对齐,但在这个例子中,我需要在左右标题中左右对齐文本。

4 个解决方案

#1


2  

I am interpreting your question as "I want to have one cell's value as the left part of my header, and another cell's value as the right part of my header".

我将你的问题解释为“我希望将一个单元格的值作为标题的左侧部分,将另一个单元格的值作为标题的右侧部分”。

If so, you probably want:

如果是这样,你可能想要:

Sub M()
    With ActiveSheet.PageSetup
        .LeftHeader = ActiveSheet.Range("A1").Value
        .CenterHeader = ""
        .RightHeader = ActiveSheet.Range("B1").Value
    End With
End Sub

#2


3  

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.

要在Excel中执行此操作,请选择对齐所需的单元格部分。然后,为左侧键入alt + H + A + L,为中心键入alt + H + A + C,为右侧键入alt + H + A + R.

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:

但是,根据您的问题,您似乎希望在VBA而不是Excel中执行此操作。如果是这种情况,请改为:

Range(myRange).HorizontalAlignment = xlRight for right, and Range(myRange).HorizontalAlignment = xlLeft for left, where myRange is the range of cells.

范围(myRange).Horizo​​ntalAlignment = xlRight表示右侧,范围(myRange).Horizo​​ntalAlignment = xlLeft表示左侧,其中myRange是单元格的范围。

#3


0  

You can use Range("A1").HorizontalAlignment = xlLeft or ... = xlRight

您可以使用Range(“A1”)。Horizo​​ntalAlignment = xlLeft或... = xlRight

Of course, adjust the range(s) as necessary.

当然,根据需要调整范围。

#4


0  

The HorizontalAlignment property of the Range should be what you're looking for. xlLeft or xlRight are values to align left or right.

Range的Horizo​​ntalAlignment属性应该是您正在寻找的。 xlLeft或xlRight是左对齐或右对齐的值。

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.

将B1细胞对齐到右边。

#1


2  

I am interpreting your question as "I want to have one cell's value as the left part of my header, and another cell's value as the right part of my header".

我将你的问题解释为“我希望将一个单元格的值作为标题的左侧部分,将另一个单元格的值作为标题的右侧部分”。

If so, you probably want:

如果是这样,你可能想要:

Sub M()
    With ActiveSheet.PageSetup
        .LeftHeader = ActiveSheet.Range("A1").Value
        .CenterHeader = ""
        .RightHeader = ActiveSheet.Range("B1").Value
    End With
End Sub

#2


3  

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.

要在Excel中执行此操作,请选择对齐所需的单元格部分。然后,为左侧键入alt + H + A + L,为中心键入alt + H + A + C,为右侧键入alt + H + A + R.

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:

但是,根据您的问题,您似乎希望在VBA而不是Excel中执行此操作。如果是这种情况,请改为:

Range(myRange).HorizontalAlignment = xlRight for right, and Range(myRange).HorizontalAlignment = xlLeft for left, where myRange is the range of cells.

范围(myRange).Horizo​​ntalAlignment = xlRight表示右侧,范围(myRange).Horizo​​ntalAlignment = xlLeft表示左侧,其中myRange是单元格的范围。

#3


0  

You can use Range("A1").HorizontalAlignment = xlLeft or ... = xlRight

您可以使用Range(“A1”)。Horizo​​ntalAlignment = xlLeft或... = xlRight

Of course, adjust the range(s) as necessary.

当然,根据需要调整范围。

#4


0  

The HorizontalAlignment property of the Range should be what you're looking for. xlLeft or xlRight are values to align left or right.

Range的Horizo​​ntalAlignment属性应该是您正在寻找的。 xlLeft或xlRight是左对齐或右对齐的值。

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.

将B1细胞对齐到右边。