PDA

View Full Version : [SOLVED] Populating the TreeView



Kaizer
01-23-2005, 10:11 AM
In column B of a sheet I have my structure that I want to present in a TreeView. In column A I determine the levels of relatinships. I understand that I need to loop through columns A and B and determine Parents and Childs and populate the TreeView. But something is not working.

Can you peek at the attached WorkBook with my structure and help with the advise?

Jacob Hilderbrand
01-23-2005, 05:30 PM
Can you explain a bit about how you want it setup? In your example, how would you like the data entered into the Tree View? Can you set it up like this?
Parent | Name
0 (0 for no parent) | Name1
Name1 | Sub1
Name1 | Sub2
0 | Name2
Name2 | Sub1

Kaizer
01-24-2005, 01:53 AM
I do not really know how to set it up, but would like to see the Tree View as shown in the attached picture.
I was trying to put Parents in column C and Child in column D in order to use in the code but it did not work for me.

Jacob Hilderbrand
01-24-2005, 03:16 AM
Is this what you want to do?

Column A is main categories with the category names starting at Row 3.
Column B is the parent category for the sub category that is listed in Column C.

Here is the code:


Option Explicit

Private Sub UserForm_Initialize()
Dim i As Long
Dim j As Long
Dim k As Long
Dim Title As String
Dim xNode As Node
Dim NodeKey As String
Dim NodeKey2 As String
j = Sheets("Sheet1").Range("A65536").End(xlUp).Row
With Me.TreeView1
For i = 3 To j
Set xNode = .Nodes.Add
NodeKey = Range("A" & i).Text
With xNode
.Key = NodeKey
.Text = NodeKey
.Expanded = False
End With
Next i
j = Sheets("Sheet1").Range("C65536").End(xlUp).Row
For i = 3 To j
Set xNode = .Nodes.Add(Range("B" & i).Text, tvwChild)
NodeKey = Range("C" & i).Text
With xNode
.Key = NodeKey
.Text = NodeKey
End With
Next i
End With
Set xNode = Nothing
End Sub

See the attachment for more information.

Kaizer
01-24-2005, 04:52 AM
Cool, it worked.

Jacob Hilderbrand
01-24-2005, 04:54 AM
Glad to help. :beerchug:

Take Care