Update Userform Label from sheet when selecting parent/child from treeview.

db74

New Member
Joined
Jul 5, 2022
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Hello. I've created a treeview using the below code, which gets the parents and childs from a sheet, but I'm struggling to get my head around updating a label depending on what node is selected. If a parent is selected then I need to update the label with the relevant cell info probably using an offset and the same for a child. I need to reference the selected node with that of the range on the sheet, so column A8:A for parent and B8:B for child. Any examples or pointers would be greatly appreciated as I'm currently banging my head against the wall. Thanks

Private Sub DirTV()

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
For Each c In Worksheets("Directory").Range("a8:a" & Range("a" & Rows.Count).End(xlUp).Row)

'Setting parents
Set nParent = DirectoryTree.Nodes.Add(, "Parent" + c.Value, c.Value, c.Value)

'Ensureing no error is there
If Err.Number <> 0 Then
'Error reset and setting parent
Err.Clear
Set nParent = DirectoryTree.Nodes(c.Value)
End If

'Setting child from col b
Set nChild = DirectoryTree.Nodes.Add(nParent, tvwChild, c.Value + c.Offset(0, 1).Value, c.Offset(0, 1).Value)

Next

End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I've managed to partially resolve the issue with two scripts, but can't figure out how to add these as an if statement, whether the node is parent or child, onto the nodeclick event. Any ideas? Thanks

VBA Code:
Private Sub DirectoryTreeParent(ByVal Node As MSComctlLib.Node)

Dim LastRowID As Long
Set LastRowID = Worksheets("Directory").Cells(Worksheets("Directory").Rows.Count, "A").End(xlUp).Row

For Each ID In Worksheets("Directory").Range(Worksheets("Directory").Cells(2, 1), Worksheets("Directory").Cells(LastRowID, "A"))

If ID.Value = Node.Text Then

DirLabel.Caption = ID.Offset(, 1).Value & vbNewLine & PID.Offset(, 2).Value

End If

Next ID
    
End If
End Sub


Private Sub DirectoryTreeChild(ByVal Node As MSComctlLib.Node)

Dim LastRowID As Long
Set LastRowID = Worksheets("Directory").Cells(Worksheets("Directory").Rows.Count, "B").End(xlUp).Row

For Each ID In Worksheets("Directory").Range(Worksheets("Directory").Cells(2, 1), Worksheets("Directory").Cells(LastRowID, "B"))

If ID.Value = Node.Text Then

DirLabel.Caption = ID.Offset(, 19).Value & vbNewLine & ID.Offset(, 1).Value

End If

Next ID
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,935
Members
449,094
Latest member
teemeren

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