Convert Levels file into Parent-Child format

aston_007

New Member
Joined
Apr 7, 2023
Messages
9
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
Hello Experts.

Hoping that you will be able to assist.

I am needing to convert the following file format that has a levels hierarchy into a Parent/Child format.
The file can have up to 8 levels. It can also be 'ragged' in that not all levels to the right will have values as per the example below:
Levels.jpg


The conversion would result in a 4 column, Parent | Child output as below:
PC.jpg

It would be useful if the Levels could be output sequentially so that all the parents are listed first with the last levels at the end.
Hope I've described that effectively!

Thanks very much all.

Mark
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi Mark, Welcome to the board!

Here's just one possible way. Try it out on a copy of your data. Change the source & destination sheets to suit.

VBA Code:
Option Explicit
Sub Parent_Child()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Worksheets("Sheet1")  '<~~ Change to actual source sheet name
    Set ws2 = Worksheets("Sheet2")  '<~~ Change to actual destination shet name
    
    Dim LCol As Long, LRow As Long, lrS As Long, totR As Long
    LCol = ws1.Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).Column
    lrS = ws1.Cells.Find("*", , xlFormulas, , xlByRows, xlPrevious).Row
    
    'Fill the input array
    Dim arrIn, nRng As Long, i As Long, j As Long
    nRng = LCol / 2
    ReDim arrIn(1 To nRng)
    With ws1
        j = 1
        For i = 1 To nRng
            LRow = ws1.Range(ws1.Cells(1, j + 1), ws1.Cells(lrS, j + 1)).Find("*", , xlFormulas, , 1, 2).Row
            arrIn(i) = ws1.Cells(2, j).Resize(LRow, 4)
            totR = totR + UBound(arrIn(i), 1)
            j = j + 2
        Next i
    End With
    
    'Fill the output array
    Dim arr, r As Long, rw As Long, col As Long
    ReDim arrOut(1 To totR, 1 To 4)
    r = 1
    For i = 1 To nRng
        arr = arrIn(i)
        For rw = 1 To UBound(arr, 1)
            If arr(rw, 3) <> "" Then
                For col = 1 To UBound(arr, 2)
                arrOut(r, col) = arr(rw, col)
                Next col
                r = r + 1
            End If
        Next rw
    Next i
    
    'Put the result onto sheet 2
    With ws2
        .Range("A1").Resize(1, 4).Value2 = Array("Parent Code", "Parent Name", "Child Code", "Child Name")
        .Range("A2").Resize(totR, 4).Value2 = arrOut
        .Range("A:D").RemoveDuplicates Columns:=Array(1, 2, 3, 4)
        .Range("A1:D1").EntireColumn.AutoFit
    End With
    Application.ScreenUpdating = True
End Sub

Sheet1:
Parent Child.xlsm
ABCDEFGHIJ
1L1 CODEL1 NAMEL2 CODEL2 NAMEL3 CODEL3 NAMEL4 CODEL4 NAMEL5 CODEL5 NAME
2GLOBALGlobal TotUKUnited KingdomLONLondonLONNNorth LondonTOTNTottenham
3GLOBALGlobal TotUKUnited KingdomLONLondonLONSSouth LondonCHEAChelsea
4GLOBALGlobal TotUKUnited KingdomMANManchesterMANSSouth Manchester
5GLOBALGlobal TotDEGermanyBERBerlinBERSSouth Berlin
6GLOBALGlobal TotDEGermanyHANHanoverHANCCentral Hanover
Sheet1


Sheet2 after running the code:
Parent Child.xlsm
ABCD
1Parent CodeParent NameChild CodeChild Name
2GLOBALGlobal TotUKUnited Kingdom
3GLOBALGlobal TotDEGermany
4UKUnited KingdomLONLondon
5UKUnited KingdomMANManchester
6DEGermanyBERBerlin
7DEGermanyHANHanover
8LONLondonLONNNorth London
9LONLondonLONSSouth London
10MANManchesterMANSSouth Manchester
11BERBerlinBERSSouth Berlin
12HANHanoverHANCCentral Hanover
13LONNNorth LondonTOTNTottenham
14LONSSouth LondonCHEAChelsea
Sheet2
 
Upvote 0
Solution
Hi Kevin.

Thanks for replying so quickly. The code that you have offered as worked amazingly well!
I've run it against 8 levels and 30k+ lines and processed it so quickly.

Many thanks!

Mark
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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