Getting data from Excel table into Treeview: ABle to get parents but not child!

haplc

Board Regular
Joined
May 27, 2004
Messages
71
Dear All,
I am trying to get data from Col A of an excel sheet as parents of Treeview. Col B and C are having value of Child. I am able to get the parents name but not able to get the child name fom COl b and C. Please see code below:



Private Sub UserForm_Initialize()

'for excel to treeview

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 and C
For Each c In Sheet3.Range("a1:b" & Range("a" & Rows.Count).End(xlUp).Row)

'Setting parents--THIS PART IS WORKING
Set nParent = TreeView2.Nodes.Add(, , c.Value, c.Value)

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

'Setting child from col b and c: THIS PART IS NOT WORKING
Set nChild = TreeView2.Nodes.Add(nParent, tvwChild, c.Offset(0, 1).Value, c.Offset(0, 2).Value)

Err.Clear
Next
 

Attachments

  • Excel data table.jpg
    Excel data table.jpg
    12.7 KB · Views: 49
  • Treeview.jpg
    Treeview.jpg
    15.8 KB · Views: 49

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this.
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
 
Upvote 0
Solution
Thanks Norie for the prompt reply. I tried but I am getting error : Run time error '35603'- Invalid key

At this line:
Set nParent = UserForm1.TreeView2.Nodes.Add(, , c.Value, c.Value)
 
Upvote 0
What type of values do you have in column A?
 
Upvote 0
Hello Norie
It is working now. I have used your lines for child in the old codes. Thanks a lot
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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