+ Reply to Thread
Results 1 to 4 of 4

Load/Populate TreeView data from sheet to Userfrom

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-12-2010
    Location
    Excel World
    MS-Off Ver
    Excel 2013, 2019 & O365
    Posts
    214

    Load/Populate TreeView data from sheet to Userfrom

    Problem >---> I have a TreeView on a sheet, and just want to load this TreeView data from sheet to a UserForm.

    Though I have googled many site, still could not get the perfect way for multiple columns, where a TreeView can have up to 200 nodes (parents+children).

    Could anyone please help me on this?
    I am middle of a project, and need this functionality, where a user can load previously saved TreeView from sheet to UserFrom.

    My Method: Launch the UserForm >> click "Load TreeView" button >> VBA Script will automatically update/load the TreeView data on UserFrom
    P.S. - I am using Excel 2010.

    Thanks in advanved!
    Attached Files Attached Files
    Last edited by SunOffice; 07-19-2013 at 05:59 PM. Reason: edited the thread title... more clear now.
    Excelforum is Completely Awesome! True learning with Live Examples & Best Techniques!!

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Load/Populate TreeView data from sheet to Userfrom

    Private Sub btnLoadTreeView_Click()
    
        '// This is used to save a reference to the Parent node
        Dim nP As Node
        
        '// General range
        Dim c As Excel.Range
        
        '// Note - must include the $'s as cell address is compared to this
        Const cROOT As String = "$B$4"
        
        '// Will handle most common errors in-line
        On Error Resume Next
        
        With TreeView1
        
            '// Clear any existing
            .Nodes.Clear
    
            '// Add the Root node. All nodes also have a key set. The .TEXT property is set
            '// to the contents of the cell. The .KEY property is the Cell address.
            '// Keys must be unique - so not using the cell contents for the key
            Set np = .Nodes.Add(, , Range(cROOT).Address, Sheet1.Range(cROOT).Value)
    
            '// Loop through all cells in the Root Cell currentRegion. This should/will
            '// include all defined cells as long as the general schema is adhered to
            '// with no totally blank rows or columns
            
            For Each c In Sheet1.Range(cROOT).CurrentRegion
    
                '// Cell contains sometihng and is not the ROOT cell?
                If c.Value <> vbNullString And c.Address <> cROOT Then
    
                    '// Find the parent node for this cell. This is the cell
                    '// 1 column to the left and .END(xlup) upwards...
                    Set nP = .Nodes(c.Offset(, -1).End(xlUp).Address)
                    
                    '// Snags - The parent should exist so schema is wrong
                    If nP Is Nothing Then
                        MsgBox "ERROR: Parent node " & c.Offset(, -1).End(xlUp).Value & " not found...", vbExclamation, "Error"
                        Exit Sub
                    End If
                    
                    '// simply add a new child to the parent - .TEXT and .KEY from the cell
                    '// text is the cell Value, Key is the Cell Address
                    .Nodes.Add nP, tvwChild, c.Address, c.Value
                    
                    '// check added OK
                    If Err.Number <> 0 Then
                        MsgBox "ERROR: The node " & c.Value & " is a duplicate. All node descrptions must be unique", vbExclamation, "Error"
                        Exit Sub
                    End If
                    
                    '// Expand the parent just so the new node is visible
                    nP.Expanded = True
                End If
    
            Next
    
            '// Select and show the ROOT node
            With .Nodes(Range(cROOT).Address)
                .Selected = True
                .EnsureVisible
            End With
            
        End With
        
        '// All done
        Exit Sub
        
    End Sub
    NOTE:
    • Your schema is incorrect. Cell A21 (The cell contents, not the Address) should be offset one row down - not directly opposite its parent, AA2 - All the others cells in the schema follow that convention. This causes AA21 to be a child of AA1 instead of AA2.
    Last edited by cytop; 07-19-2013 at 08:55 AM. Reason: Revised to use cell address as Key - guaranteed unique so some code removed.

  3. #3
    Forum Contributor
    Join Date
    08-12-2010
    Location
    Excel World
    MS-Off Ver
    Excel 2013, 2019 & O365
    Posts
    214

    Re: Load/Populate TreeView data from sheet to Userfrom

    Thank you so much for your all genius efforts!!
    I am impressed by your logics.
    Yes! You are absolutely right for the position of A21 and A22. I have corrected it in the file.
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    06-10-2015
    Location
    Rotorua, New Zealand
    MS-Off Ver
    2010
    Posts
    1

    Re: Load/Populate TreeView data from sheet to Userfrom

    Hi there, I am using Excel 2010 but can't get Treeview to work in userforms because Excel can't load the Treeview object. Do you have a workaround for this? Or a link to the file I can add in?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 07-16-2012, 10:16 AM
  2. load data from 2 userforms to 1 sheet at the same time
    By ncaravela in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-24-2010, 06:53 AM
  3. load highlighted data from one sheet to another
    By dealer in forum Excel General
    Replies: 2
    Last Post: 06-07-2010, 04:48 AM
  4. [SOLVED] Treeview on a sheet
    By PatrickS in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-03-2006, 04:40 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1