Excel Treeview VBA code not showing additional child nodes

db74

New Member
Joined
Jul 5, 2022
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hi. I'm trying to display parent and child codes with the below code, but it seems that any child nodes after a single parent node do not display. If I move all the parent and child nodes to the top of the sheet then it works.

Any advice would be appreciated. Thanks

An example of my data is as follows:

Parent Child
ParentOne ChildOne
ParentOne ChildTwo
ParentOne ChildThree
ParentTwo
ParentThree ChildOne
ParentThree ChildTwo

VBA Code:
Private Sub UserForm_Initialize()
Dim c As Range
Dim nParent As Node
Dim nChild As Node

'Parents are stored in Col A and child are stored in col B and C
For Each c In Sheet1.Range("a1:a" & Range("a" & Rows.Count).End(xlUp).Row)

'Setting parents--THIS PART IS WORKING
Set nParent = TreeView1.Nodes.Add(, , c.Value, c.Value)
'Setting child from col b and c: THIS PART IS NOT WORKING
Set nChild = TreeView1.Nodes.Add(nParent, tvwChild, c.Offset(0, 1).Value, c.Offset(0, 1).Value)
Set nChild = TreeView1.Nodes.Add(nParent, tvwChild, c.Offset(0, 2).Value, c.Offset(0, 2).Value)
Err.Clear
 Next c
    
End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
The issue was down to the child data not being unique although the parent was, so I've amended the code to include the parent in the ID, well that's what I think I've done.

Private Sub DirTV()

Dim c As Range
Dim nParent As Node
Dim nChild As Node

'ON error next
On Error Resume Next

'Parents are stored in Col A and child are stored in col B
For Each c In Worksheets("Directory").Range("a8:a" & Range("a" & Rows.Count).End(xlUp).Row)

'Setting parents
Set nParent = DirectoryTree.Nodes.Add(, "Parent" + c.Value, c.Value, c.Value)

'Ensureing no error is there
If Err.Number <> 0 Then
'Error reset and setting parent
Err.Clear
Set nParent = DirectoryTree.Nodes(c.Value)
End If

'Setting child from col b
Set nChild = DirectoryTree.Nodes.Add(nParent, tvwChild, c.Value + c.Offset(0, 1).Value, c.Offset(0, 1).Value)

Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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