Generating XML tags in Excel using Levels column in VBA

vds1

Well-known Member
Joined
Oct 5, 2011
Messages
1,194
Hello Gurus,

Hope your staying safe, posting after long time.

so I have a sheet with table that contains Level, XML tag and Value in Column A, B and C respectively as shown below in Sheet named as "Master"

LEVEL (COLUMN A)XML TAG (COLUMN B)VALUE (COLUMN C)
1​
<AppHdr>
2​
<Fr>
3​
<OrgId>
4​
<Id>
5​
<OrgId>
6​
<Othr>
7​
<Id>50
2​
<To>
3​
<OrgId>
4​
<Id>
5​
<OrgId>
6​
<Othr>
7​
<Id>60
2​
<BMsgIdr>MSG1
2​
<MDefIdr>MSG2

The output will be multi level XML tags in new sheet starting from column A

<AppHdr>
<Fr>
<OrgId>
<Id> 50
<OrgId>
<Othr>
<Id> 50
</Id>
</Othr>
</OrgId>
</Id>
</OrgId>
</Fr>
<To>
<OrgId>
<Id> 51
<OrgId>
<Othr>
<Id> 60
</Id>
</Othr>
</OrgId>
</Id>
</OrgId>
</To>
<BMsgIdr> MSG1
</BMsgIdr>
<MDefIdr> MSG2
</MDefIdr>
</AppHdr>

Any guidance is much appreciated.. Thank you for looking into it
 

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Bo_Ry

Board Regular
Joined
Oct 27, 2018
Messages
140
Office Version
  1. 365
Platform
  1. Windows
Please create a new Result Sheet and try

VBA Code:
Sub XMLtag()
Dim c As Range, r As Long, i As Long, dic As Object
Sheets("Master").Cells.Replace What:=ChrW(8203), Replacement:=""
Set dic = CreateObject("Scripting.Dictionary")
r = 1
With Sheets("Result")
    .UsedRange.Clear
    For Each c In Sheets("Master").Range("A2", Sheets("Master").Range("A2").End(xlDown))
        .Cells(r, c.Value) = c.Offset(, 1) & c.Offset(, 2)
        If dic.Exists(c.Value) Then
            dic(c.Value) = "</" & Mid(c.Offset(, 1), 2, 20)
        Else
            dic.Add c.Value, "</" & Mid(c.Offset(, 1), 2, 20)
        End If
        If c >= c.Offset(1) Then
            For i = c To Application.Max(1, c.Offset(1)) Step -1
                r = r + 1
                .Cells(r, i) = dic(i)
            Next i
        End If
        r = r + 1
    Next c
End With
End Sub
 
Solution

vds1

Well-known Member
Joined
Oct 5, 2011
Messages
1,194
Please create a new Result Sheet and try

VBA Code:
Sub XMLtag()
Dim c As Range, r As Long, i As Long, dic As Object
Sheets("Master").Cells.Replace What:=ChrW(8203), Replacement:=""
Set dic = CreateObject("Scripting.Dictionary")
r = 1
With Sheets("Result")
    .UsedRange.Clear
    For Each c In Sheets("Master").Range("A2", Sheets("Master").Range("A2").End(xlDown))
        .Cells(r, c.Value) = c.Offset(, 1) & c.Offset(, 2)
        If dic.Exists(c.Value) Then
            dic(c.Value) = "</" & Mid(c.Offset(, 1), 2, 20)
        Else
            dic.Add c.Value, "</" & Mid(c.Offset(, 1), 2, 20)
        End If
        If c >= c.Offset(1) Then
            For i = c To Application.Max(1, c.Offset(1)) Step -1
                r = r + 1
                .Cells(r, i) = dic(i)
            Next i
        End If
        r = r + 1
    Next c
End With
End Sub


YOU ARE A CHAMP! THANK YOU VERY MUCH
 

Watch MrExcel Video

Forum statistics

Threads
1,122,822
Messages
5,598,307
Members
414,224
Latest member
Crazy_FC

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top