Generating XML tags in Excel using Levels column in VBA

vds1

Well-known Member
Joined
Oct 5, 2011
Messages
1,200
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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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
 
Upvote 0
Solution
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
 
Upvote 0

Forum statistics

Threads
1,214,971
Messages
6,122,517
Members
449,088
Latest member
RandomExceller01

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
Back
Top