VBA TreeView Control

AndyJM87

New Member
Joined
Aug 31, 2018
Messages
28
Hello,

I have a Tree View control on a user form, which pulls the nodes from a Data sheet.

For some reason, it is removing duplicates.

VBA Code:
Private Sub UserForm_Initialize()
    Dim cell As Range
    Dim nParent As Node
    Dim nChild As Node
    Application.ScreenUpdating = False
    
    On Error Resume Next
    
     '// Range
    For Each cell In Sheets("DATA").Range("A5:A15000")
        
        Set nParent = tv.Nodes.Add(, , cell.Value, cell.Value)
        
         '// Error handler
        If Err.Number <> 0 Then
            Err.Clear
            Set nParent = tv.Nodes(cell.Value)
        End If
        
         '// Subcategories
        Set nChild = tv.Nodes.Add(nParent, tvwChild, cell.Offset(0, 1).Value, cell.Offset(0, 1).Value)

        Err.Clear
        
    Next
    
    For Each cell In Sheets("DATA").Range("B5:B15000")
        Set nParent = tv.Nodes.Add(, , cell.Value, cell.Value)
        
        If Err.Number <> 0 Then
            Err.Clear
            Set nParent = tv.Nodes(cell.Value)
        End If
        
        Set nChild = tv.Nodes.Add(nParent, tvwChild, cell.Offset(0, 1).Value, cell.Offset(0, 1).Value)
        Err.Clear
        
    Next
    
    For Each cell In Sheets("DATA").Range("C5:C15000")
        Set nParent = tv.Nodes.Add(, , cell.Value, cell.Value)
        
        If Err.Number <> 0 Then
            Err.Clear
            Set nParent = tv.Nodes(cell.Value)
        End If
        
        Set nChild = tv.Nodes.Add(nParent, tvwChild, cell.Offset(0, 1).Value, cell.Offset(0, 1).Value)
        Err.Clear
        
    Next
    
    For Each cell In Sheets("DATA").Range("D5:D15000")
        Set nParent = tv.Nodes.Add(, , cell.Value, cell.Value)
        
        If Err.Number <> 0 Then
            Err.Clear
            Set nParent = tv.Nodes(cell.Value)
        End If
        
        If cell.Offset(0, 1).Value = "_(blank)" Then
            Set nChild = tv.Nodes.Add(nParent, tvwChild, cell.Offset(0, 2).Value, cell.Offset(0, 2).Value)
        ElseIf cell.Offset(0, 1).Value <> "_(blank)" Then
            Set nChild = tv.Nodes.Add(nParent, tvwChild, cell.Offset(0, 1).Value, cell.Offset(0, 1).Value)
        End If
        
        Err.Clear
        
    Next
    
    tv.Nodes.Item("SV").Expanded = True
    
    Application.ScreenUpdating = True
End Sub

So Range C has 4 options which expand. Range D contains duplicates as the 4 options in Range C require the same options in Range D.

Hope this makes sense.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,215,456
Messages
6,124,939
Members
449,197
Latest member
k_bs

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