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

haplc

Board Regular
Joined
May 27, 2004
Messages
70
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: 3
  • Treeview.jpg
    Treeview.jpg
    15.8 KB · Views: 3

Some videos you may like

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,263
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

haplc

Board Regular
Joined
May 27, 2004
Messages
70
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)
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,263
Office Version
  1. 365
Platform
  1. Windows
What type of values do you have in column A?
 

haplc

Board Regular
Joined
May 27, 2004
Messages
70
Hello Norie
It is working now. I have used your lines for child in the old codes. Thanks a lot
 

Watch MrExcel Video

Forum statistics

Threads
1,127,089
Messages
5,622,641
Members
415,916
Latest member
eugenia

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
Top