Consulting

Results 1 to 6 of 6

Thread: Populating the TreeView

  1. #1
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location

    Question Populating the TreeView

    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?

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    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

  3. #3
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    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.

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    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.

  5. #5
    VBAX Regular
    Joined
    Dec 2004
    Posts
    93
    Location
    Cool, it worked.

  6. #6
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Glad to help.

    Take Care

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •