Excel – VBA: Align cell values in vba code

excelvba

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.

Best Solution

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
Related Question