SHowing Parent-Child Relationship using VBA

_curtis

New Member
Joined
Jul 31, 2017
Messages
2
Hi there!

I am fairly new to VBA in Excel and was hoping for some guidance on this problem. I am given hierarchical data from a database to perform a report, and the user would like to format the information such that an entry's children lie directly below it (a parent can have multiple children, child has only one parent). This could perhaps be done by indenting the Notes tab of the data by a few spaces. I was wondering how to first perform the reordering of the data by hierarchy, and then visually showing an indentation by adding, say, 5 spaces to the beginning of the Notes tab.
For sample data:

[TABLE="class: cms_table, width: 500"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Parent ID[/TD]
[TD]Notes[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]NULL[/TD]
[TD]This is number 0.[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]0[/TD]
[TD]This is number 1.[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]0[/TD]
[TD]This is number 2.[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]1[/TD]
[TD]This is number 3.[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]3[/TD]
[TD]This is number 4.[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]0[/TD]
[TD]This is number 5.[/TD]
[/TR]
</tbody>[/TABLE]



I would then like the data to be presented as such:
[TABLE="class: cms_table, width: 500"]
<tbody>[TR]
[TD]ID[/TD]
[TD]Parent ID[/TD]
[TD]Notes[/TD]
[/TR]
[TR]
[TD]0[/TD]
[TD]NULL[/TD]
[TD]This is number 0.[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]0[/TD]
[TD]-----This is number 1.[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]0[/TD]
[TD]-----This is number 3.[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]1[/TD]
[TD]----------This is number 4.[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]3[/TD]
[TD]-----This is number 2.[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]0[/TD]
[TD]-----This is number 5.[/TD]
[/TR]
</tbody>[/TABLE]


Thank you so much! Your help is greatly appreciated :smile:
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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