Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a Node in a TreeView with VBA

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?

like image 212
daharon Avatar asked Jan 19 '09 23:01

daharon


People also ask

How do you select a node in TreeView vb net?

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.

How do I use TreeView in Excel VBA?

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.


2 Answers

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 
like image 57
Arnold Avatar answered Oct 03 '22 23:10

Arnold


In my Excel works:

TreeView1.Nodes(key).Selected = True
like image 39
Zdzisław Kes Avatar answered Oct 03 '22 23:10

Zdzisław Kes