Speed up parent child hierarchy macro

sachin483

Board Regular
Joined
Mar 31, 2015
Messages
157
Office Version
  1. 2019
Platform
  1. Windows
i have macro for parent child hierarchy macro but i want to run the same for more than 20000 rows can it be speed up

VBA Code:
Option Explicit

Const SourceDivCol = 1
Const SourcePosCol = 2
Const SourceRepCol = 3
Const SourceLevCol = 4
Const SourceEmpCol = 5
Const SourceCodCol = 6
Const TargetDivCol = 15
Const TargetLevCol = 16
Const TargetPosCol = 17
Const TargetEmpCol = 18
Const TargetCodCol = 19
Dim SourceRow As Long
Dim TargetRow As Long

Sub CreateReport()
    Dim Boss As Range
    Dim Adr As String
    Dim Pos As String
    Application.ScreenUpdating = False
    TargetRow = 2
    Set Boss = Columns(SourceLevCol).Find(What:=1, LookAt:=xlWhole)
    Adr = Boss.Address
    Do
        SourceRow = Boss.Row
        TargetRow = TargetRow + 1
        Cells(TargetRow, TargetDivCol).Value = Cells(SourceRow, SourceDivCol).Value
        Cells(TargetRow, TargetLevCol).Value = Cells(SourceRow, SourceLevCol).Value
        Cells(TargetRow, TargetPosCol).Value = Cells(SourceRow, SourcePosCol).Value
        Cells(TargetRow, TargetEmpCol).Value = Cells(SourceRow, SourceEmpCol).Value
        Cells(TargetRow, TargetCodCol).Value = Cells(SourceRow, SourceCodCol).Value
        Pos = Cells(SourceRow, SourcePosCol).Value
        Call AddChildren(Pos)
        Set Boss = Columns(SourceLevCol).Find(What:=1, After:=Boss, LookAt:=xlWhole)
        If Boss Is Nothing Then Exit Do
    Loop Until Boss.Address = Adr
    Application.ScreenUpdating = True
End Sub

Sub AddChildren(BossPos As String)
    Dim Child As Range
    Dim Adr As String
    Dim Pos As String
    Set Child = Columns(SourceRepCol).Find(What:=BossPos, LookAt:=xlWhole)
    If Child Is Nothing Then Exit Sub
    Adr = Child.Address
    Do
        SourceRow = Child.Row
        TargetRow = TargetRow + 1
        Cells(TargetRow, TargetDivCol).Value = Cells(SourceRow, SourceDivCol).Value
        Cells(TargetRow, TargetLevCol).Value = Cells(SourceRow, SourceLevCol).Value
        Cells(TargetRow, TargetPosCol).Value = Cells(SourceRow, SourcePosCol).Value
        Cells(TargetRow, TargetEmpCol).Value = Cells(SourceRow, SourceEmpCol).Value
        Cells(TargetRow, TargetCodCol).Value = Cells(SourceRow, SourceCodCol).Value
        Pos = Cells(SourceRow, SourcePosCol).Value
        Call AddChildren(Pos)
        Set Child = Columns(SourceRepCol).Find(What:=BossPos, After:=Child, LookAt:=xlWhole)
        If Child Is Nothing Then Exit Do
    Loop Until Child.Address = Adr
End Sub
 
if level no. is sorted from largest to small then it doesn't work
That is very simple to solve, just sort your data before running the macro.

The important thing is 2 points:
1. Improve speed.
2. That the macro works with the situation that you raised in your original post.

Undoubtedly the macro could fail if you change the setting, if you put the data in a different way, if you change the columns, if they start in another row, if you protect the sheet, in short, a number of possibilities that I could not know.

We would like you to comment on your original request:
i have macro for parent child hierarchy macro but i want to run the same for more than 20000 rows can it be speed up
Is the time faster with that amount of rows?

--------------
I hope to hear from you soon.
Respectfully
Dante Amor
--------------
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Thanks ,the speed is fast but where there are multiple division at level 1 2nd division level gets deleted ie:- YY level does not gets displayed

DATA
DIVISIONPOSITIONPOSITION REPORTINGLEVEL_NOempnocode
XXOZ00301INDOL
1​
E1LL81
YYYZ00301INDOT
1​
E1YY81
XXLR0201OZ00301
2​
E2LL82
YYYR0201OZ00301
2​
E2YY82
XXLA0101LR0201
3​
E3LL83
XXLA0201LR0201
3​
E4LL84
XXLA0701LR0201
3​
E5LL85
YYYA0101YR0201
3​
E3YY83
YYYA0201YR0201
3​
E4YY84
YYYA0701YR0201
3​
E5YY85
XXXX0502LA0201
4​
E6LL86
XXXX0601LA0201
4​
E7LL87
XXXX1901LA0201
4​
E8LL88
XXXX2101LA0201
4​
E9LL89
XXXX2201LA0701
4​
E10LL90
XXXX0101LA0101
4​
E11LL91
XXXX0102LA0101
4​
E12LL92
XXXX0103LA0101
4​
E13LL93
XXXX0104LA0101
4​
E14LL94
XXXX0201LA0701
4​
E15LL95
XXXX0301LA0701
4​
E16LL96
XXXX0501LA0201
4​
E17LL97
YYYY0502YA0201
4​
E6YY86
YYYY0601YA0201
4​
E7YY87
YYYY1901YA0201
4​
E8YY88
YYYY2101YA0201
4​
E9YY89
YYYY2201YA0701
4​
E10YY90
YYYY0101YA0101
4​
E11YY91
YYYY0102YA0101
4​
E12YY92
YYYY0103YA0101
4​
E13YY93
YYYY0104YA0101
4​
E14YY94
YYYY0201YA0701
4​
E15YY95
YYYY0301YA0701
4​
E16YY96
YYYY0501YA0201
4​
E17YY97
 
Upvote 0
but where there are multiple division at level 1 2nd division level gets deleted

As I said there are several factors that I do not know, in my head I only conceive a structure with a level 1. I can't imagine a country with 2 presidents, a company with 2 directors, etc. But I guess this case is different, but you didn't mention it, and I can't develop something for cases that you didn't ask for and probably don't exist.

Please share a file with a more real case, if possible your 20,000 rows file, so that I can plan, design, develop and test a macro according to your real needs.
;)

Or maybe someone else in the forum, already with all the requirements well defined, will help us with a code that works for you. :cool:
 
Upvote 0

Forum statistics

Threads
1,215,167
Messages
6,123,401
Members
449,098
Latest member
ArturS75

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