TreeView Structure in Excel VBA
TreeView is extreamly useful in specific cases but it can be bit tricky sometimes to implement.
This post takes you through the basic operations to create and operate a TreeView.
It will be like the Folder tree window of the windows explorer. Moreover, you can have it dynamically updated based on the data in excel....
Here we go..
Idea was to populate the tree completly on the fly from the data defined in the sheet. change the structure and content in the sheet and the TreeView gets updated automatically...
Get to the VBA Window (Press <ALT+F11>,
Right Click on the ToolBox Window and
Select Additional Controls...
Browse through the window and
select "Microsoft TreeView Control ...." and "Microsoft ImageList Control ....".
Now these two items would have come in your TooBox.
Select and place it on the Form and start working.
2. Name the Tree:
I have changed the name of the tree to TV from the default TreeView1.
Hereon, I will use TV for that item.
3. Build the Tree up:
You build tree by adding nodes. You can add a node as a Child or a Parent etc..
I like to use the relationship - Child.
Basically, each node has a
..... Key (like the name of controls),
..... Text (like the Caption of controls),
..... Index (the ListIndex of a ListBox)
3.1 Adding Node
You create each node in relation to another.
TV.Nodes.Add(Relative:="ThekeyofParent", Relationship:=tvwChild, Key:="NewKey", Text:="New Text")
The base Node can be added as..
TV.Nodes.Add( , , "Key001", "The Base")
The Next Node can be added as..
TV.Nodes.Add("Key001", tvwChild, "Key011", "The First Child")
3.2 Removing Node
Remove the Node by refering to the Key or the Index of the item.
TV.Nodes.Remove(Key)
TV.Nodes.Remove
3.3 Clear the Nodes
TV.Nodes.Clear
4. Usual properties of Nodes:
You can refer to a node with its Key or Index.
..... Key > TV.Nodes(i).Key
..... Text > TV.Nodes(i).Text or simply TV.Nodes(i)
..... Index > TV.Nodes(key).Index
' Gives the Key of the Node.
TV.Nodes(key).Index
' Gives the Index of the the Node.
TV.Nodes(key).children
' Gives the No of children it has.
etc...
Its safe to use Int(i) and str(Key) when you are using other variable to refer to index or key.
5. Nodes Handling:
Defining the variable for assigning Nodes
Dim xNode As MSComctlLib.Node
Usual ways of assigning a node to the variable..
Set xNode = TV.Nodes(i)
Set xNode = TV.Nodes(key)
Set xNode = TV.SelectedItem
Get a parent Node or Child Node and cycle through the Nodes...
To assign xNode to the Parent of the selected item..
Set xNode = TV.SelectedItem.Parent
To assign xNode to the First Child of the selected item..
Set xNode = TV.SelectedItem.Child
To assign xNode to the Parent of the item..
Set xNode = xNode.Parent
To assign xNode to the First Child of the item..
Set xNode = xNode.Child
To assign xNode to the Next item of the same level and same parent
Set xNode = xNode.Next
To assign xNode to the First item of the same level and same parent
Set xNode = xNode.FirstSibling
To assign xNode to the Last item of the same level and same parent
Set xNode = xNode.LastSibling
Remember to Check the node existance before start using it..
Check if Node is selected or assigned properly..
If Node Is Nothing Then
MsgBox "Node not Selected or nonexistant!"
Exit Sub
End If
Further Reading :
http://puremis.net/excel/code/080.shtml
.
Comments
Maybe you can also use something like the following to convert the number to text..
"Key" + Format(Key, "00")
you let me know how far you have reached.
Regards
Joe