I have a TreeView
within a UserForm
in Excel.
When a Node is selected from the TreeView
, a ListBox
is populated with data.
When an item in the ListBox
is double-clicked, a separate UserForm
is shown which allows the user do to stuff.
Once the user returns back to the TreeView UserForm
, I want the Node
that was selected previously to be highlighted.
The problem is that the UserForm
basically resets itself, and I can't figure out how to select a Node
with VBA.
I'm at the point where I'm debating on whether or not I can just manually fire a NodeClick
event, as everything else I've tried has failed.
Any tips?
For setting the selected node in a TreeView you call TreeView. SelectedNode to the TreeNode you want to select. then it should select the node you just created.
Click on the Developer ribbon at the top of Excel. Now click on Visual Basic in the Coding panel to open up the VBA Editor. When your editor opens, click on Insert > User Form from the menu bar at the top. In the properties panel for the new form, change the Name property to Nations.
I know this is an old question but i had trouble enough finding a decent answer myself so here we go. Index or Key's are not always available. On create I place the fullpath of the node in the node.tag. On double-click I recall the tag value, later on I search the treeview node collection for the tag. To much? not efficient? maybe but its works likes a charm every time and i can use my own identifiers in the tag based on the purpose of the treeview. The find code:
Sub MyTreeview_FindNode(strKey As String)
Dim myNode As Node
For Each myNode In Me.Treeview.Nodes
If myNode.Tag = strKey Then
myNode.Selected = True
myNode.EnsureVisible
End If
Next
End Sub
In my Excel works:
TreeView1.Nodes(key).Selected = True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With