VBA:在 vba 代码中对齐单元格值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16813833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:19:35  来源:igfitidea点击:

VBA: Align cell values in vba code

excel-vbavbaexcel

提问by user2144293

How can I align(center) my cell values for my below code?

如何为我的以下代码对齐(居中)我的单元格值?

For x = 0 To EleXML.ChildNodes.Length - 1    
      Range("A10").offset(x,0) = EleXML.ChildNodes.Item(x).getAttribute("aa")
      Range("A10").offset(x,0) = EleXML.ChildNodes.Item(x).getAttribute("bb")
      Range("A10").offset(x,0) = EleXML.ChildNodes.Item(x).getAttribute("cc")
Next x

Please help,thanks in advance.

请帮忙,提前致谢。

采纳答案by Santosh

Is it what you are looking for ?

这是您要找的吗?

s = 1
For x = 0 To EleXML.ChildNodes.Length - 1
    Range("A10").Offset(s, 0) = EleXML.ChildNodes.Item(x).getAttribute("aa")
    Range("A10").Offset(s + 1, 0) = EleXML.ChildNodes.Item(x).getAttribute("bb")
    Range("A10").Offset(s + 2, 0) = EleXML.ChildNodes.Item(x).getAttribute("cc")
    s = s + 3
Next x

You can figure this out by recording the code.

你可以通过记录代码来解决这个问题。

Sub Macro1()
'
' Macro1 Macro
'

'
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
End Sub

Try this. You can adjust this as per your need.

尝试这个。您可以根据需要进行调整。

With Range("A10").Offset(x, 0)
    .HorizontalAlignment = xlLeft
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With