Most Valuable Professional

View Jan Karel Pieterse's profile on LinkedIn subscribe to rss feed
Subscribe in a reader

Subscribe to our mailing list

* indicates required

File crashes Excel!!

Save my workbook!
The best tool for salvaging problematic workbooks.


Excel VBA Masterclass (English)
Excel VBA for Financials (Dutch)

Third party tools

Speed up your file

The best tool to optimise your Excel model!

What has changed?

PerfectXL Compare, for Spreadsheet Comparison
This Excel compare tool is fast, reliable, and easy to use. Compare two Excel files now and see for yourself!
Home > English site > Articles > Treeview control > Features

An MSForms (all VBA) treeview; Features


Realistically a VBA form with all the controls we need for each node can’t be expected to match that of the compiled original. Even so we are surprised at what has been possible, albeit with considerable tuning and refining. Light testing suggests 1000 to 2000 nodes is quite feasible even in a typical five year old laptop, depending on the configuration of the treeview.


The original has a wide variety of features and functionality. We have tried to replicate most of these, and added a few features of our own.


The common controls treeview has a checkboxes option so you can check nodes. We ensured our treeview has that too.

A picture says more than a thousand words, so:

Checkboxes are possible and work
Checkboxes are possible and work!

Expand/collapse boxes or icons

By default we draw a rectangle with a plus or minus characters as text to mimic the expand/collapse boxes. You can also opt for expand/collapse icons. We have bundled XP and Win7 style expander icons in the demo form or you can customize your own as you wish:

 Expander icons are provided
Icons as expand/collapse buttons


The treeview from the common controls lib always shows lines to indicate the structure. With ours it is optional to show lines. The choice might be for aesthetic reasons, however a very large treeview will load significantly faster without lines so that might be an overriding consideration.

Choose whether or not to show lines
Choose whether or not to show lines


Again, let's just look at the picture!

Icons can be added
You can add icons too!

If you are familiar with the ImageList control we have incorporated a similar approach. You can store your images in a (hidden) frame and pass a reference to the frame, just as you would pass a reference to an ImageList to the common controls treeview. However your icon images could be stored anywhere. As long as you can retrieve a “StdPicture” handle for each icon and add it to a “collection” that’s all we need.

Label formatting

Label formatting is available to you with code as simple as:

cNode.Bold = True
cNode.BackColor = RGB(255, 255, 220)    ' pale yellow
cNode.ForeColor = RGB(180, 0, 0)    ' dark red

Make sure you do not use code like:

cNode.Control.BackColor = RGB(255, 255, 220)    ' pale yellow

When you are building the set of nodes, the controls of the treeview have not yet been created (this is done when .Refresh is called). Using the properties the node class itself exposes ensures those are applied when the nodes are created. Only once the treeview is fully initialised and refreshed you can access the properties of the cNode.Control.

In addition, the treeview will automatically pick up the default font settings of its parent (frame) control, so controlling appearance of the treeview starts as easy as setting properties of the frame you'll use to host the treeview. I'll explain this on the next page.


We've already implemented quite some functionality. Here is what you can do "out of the box":


Collapsing and expanding

Clicking the expand/collapse buttons does as advertised: it expands or collapses the tree.

Navigation using mouse and keyboard

Navigation using mouse and keyboard works just like in the "old" treeview. Right and left arrow expand or collapse, up and down arrow move up or down the tree. Page-up and down just work.

Editing nodes

You can use F2 to edit a node. Double-clicking a node also puts it in edit mode. This option can be enabled or disabled by setting a property value.

Drag and drop

We're working on that.

Copying, moving (cutting) and pasting

Copy and move functions are built in, you can copy or move single nodes or entire branches. You can do that silently but we have also made it easy for you to trap user actions on the selected node, eg Ctrl-c, Ctrl-x, Ctrl-v. You can include your own code to validate the intended actions, eg only allow copy between similar levels, then instigate the copy or move.


An efficient sort routine has been devised especially for this treeview. Currently it only operates at a given node level, however options for sort order and text/binary are included.


There is quite a number of properties you can change. To give you an idea, some are listed below. There are more properties and methods, which are documented in the Excel download.


Full Width

True: Node caption widths extend beyond the width of the treeview, makes for clear highlighting of the selected node and if backcolor's are applied.

False: label widths are 'autosized' to the text
If node icons are used an additional image control is created for use with 'full width'

A large tree using node icons will load faster if 'full width' is not applied.


True: show checkboxes.
Press spacekey or click to change the value.

The check change event is trapped in the form, and the info label updated.

Allow Editing

User can manually edit the node.
Double click or press F2 on a selected node.

Key Enter to apply the edit or Esc to abort.

Show Lines

Whether or not to show lines may be a matter of aesthetics or needs depending on the tree.
Try experimenting with different indentation widths (see the spin button).

In a large tree start-up time will be considerably faster if lines are not shown.

Root button

True: the entire tree can be collapsed to the Root.
False: the tree can only be collapsed to the first level.

Expander icons

The expander buttons can be Labels with +/- characters toggled, or pairs of icons.
If icons are used the images must be available in the workbook, on a sheet or as in the demo hidden on the form.

The demo includes pseudo WinXP and Win7 Explorer type expander icons, but you can use any icons of your choice, or no icons.


Default is 15 points


The treeview already automatically adjusts its Node height to the Font size or checkbox size or Icon size, whichever is the larger.

It's probably best not to adjust Node height.

Font size

The font size and other default label properties are adopted from the parent frame.

Change at design time only.


The root nodes of the tree


All Nodes of your tree




Activate a node based on the key.


Copies the node put on the 'clipboard'


Move source node + children to destination node.


Equivalent to Treeview.Nodes.Clear.


Remove Node, its children and grandchildren.


Adds and displays all the controls for the Treeview for the first time. ALso displays and repositions node controls as required.


Updates the expanded properties according to lLevel.
Called recursively.


Removes the treeview from memory.
Must be called before your userform goes out of scope.


Ensures the node passed to the method is scrolled into view.


Currently we have made available a limited set of events:


Click event

Fires when you click any node.

Node check event

Fires when you click the checkbox of a node.

After label edit event

Fires after editing a label.

Key down event

Fires when you press a key. The demoform included in the sample project shows how to implement copy and paste.

Mouse events

All mouse events are pushed through using a single MouseEvents event.

Add your own events

It isn't hard to add your own events, e.g. it takes three steps to add a new event for a specific element of the tree:

  1. Add event to the clsNode class for the piece of the tree you need an event for, by using the dropdowns at the top of the code window. Make sure it then calls a Friend sub in the clsTreeView class.
  2. Add an Event declaration to the clsTreeView class (like shown above) and a friend sub that raises that event when called from clsNode.
  3. Add an event handler to your userform.

Next: using this treeview in your VBA project.




All comments about this page:

Comment by: Gill (28-2-2013 16:43:27)

Great work guys! :)
I'm not sure if this is a bug or a deliberate design feature, but I notice that the "NodesClear" method also sets the "TreeControl" object to Nothing.
This caused my project to crash several times until I worked out what was going on... I clear out and redraw the treeview whenever a change is made so it would be good if you could clear out all the nodes whilst leaving everyting else intact.
I realise it's not too difficult to work round, but like a lot of people I'm a disciple of PED and my code borrows heavily from the "PETRAS" application, so I'm sure others will have the same problem!


Comment by: Jan Karel Pieterse (1-3-2013 15:36:15)

Hi Gill,

Thanks for letting us know!
I agree that nodesclear should not be clearing the pointer to the treecontrol.

We'll update the code as soon as we can to overcome this.


Comment by: Salim (28-3-2013 09:15:51)

Jan Karel Pieterse,

Your TreeView Control is really quite amazing. You really deserve an award for this. I just hope we could one day create a custom GridFlex Control. I just realised on the TreeView Control that when a node is already active, clicking on it the second time does not trigger the click event. Just to let you know.

Thanks & warm regards,
Salim T. S.


Comment by: Jan Karel Pieterse (28-3-2013 15:26:38)

Hi Salim,

Thank you very much for your compliment!


Comment by: Greg Bakker (6-7-2013 17:35:48)

Salim mentioned that "when a node is already active, clicking on it the second time does not trigger the click event". I ran into this behavior also and did some digging to see if I could change the behavior. It looks like Jan/Ben anticipated that people may want this functionality. In the clsNode.mctlControl_Click() event procedure there is a comment that says that if it is preferred that the click event is always raised to the form (even if the node was previously active) simply comment or remove this If/EndIf check.

--Greg Bakker


Comment by: Eugene (28-8-2013 12:48:38)

Excellent work. Here is a recursive method that I found on a Microsoft site which shortens the code and works well in Access.

Option Compare Database
Private Sub Form_Load()
Const strTableQueryName = "Your_tbl _Name"
Dim db As DAO.Database, rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset(strTableQueryName, dbOpenDynaset, dbReadOnly)
AddBranch rst:=rst, strPointerField:="Parent", strIDField:="Id", strTextField:="Description"
End Sub
'Recursive Procedure to add branches
Sub AddBranch(rst As Recordset, strPointerField As String, strIDField As String, strTextField As String, Optional varReportToID As Variant)
Dim nodCurrent As Node, objTree As TreeView
Dim strCriteria As String, strText As String, strKey As String
Dim nodParent As Node, bk As String
Set objTree = Me!xTree.Object
If IsMissing(varReportToID) Then ' Root Branch.
strCriteria = strPointerField & " Is Null"
Else ' Search for records pointing to parent.
strCriteria = BuildCriteria(strPointerField, _
rst.Fields(strPointerField).Type, "=" & varReportToID)
Set nodParent = objTree.Nodes("a" & varReportToID)
End If
' Find the first emp to report to the boss node.
rst.FindFirst strCriteria
Do Until rst.NoMatch
' Create a string with LastName.
strText = rst(strTextField)
strKey = "a" & rst(strIDField)
If Not IsMissing(varReportToID) Then 'add new node to the parent
Set nodCurrent = objTree.Nodes.Add(nodParent, tvwChild, strKey, strText)
Else    ' Add new node to the root.
Set nodCurrent = objTree.Nodes.Add(, , strKey, strText)
End If
' Save your place in the recordset so we can pass by ref for speed.
bk = rst.Bookmark
' Add employees who report to this node.
AddBranch rst, strPointerField, strIDField, strTextField, rst(strIDField)
rst.Bookmark = bk     ' Return to last place and continue search.
rst.FindNext strCriteria ' Find next employee.
End Sub


Comment by: Antonio (17-9-2013 22:52:57)


I would know if is possible to emulate a Selected property, or anycase, how can I highlight a node.

I've been working in a search procedure (outside your code), and if found, I nedd to highlight the founded node.

Many thanks for your great job.


Madrid (Spain)


Comment by: Jan Karel Pieterse (18-9-2013 10:11:09)

Hi Antonio,

You can simply set the ActiveNode, as in:

Set mcTree.ActiveNode = MyFoundNode


Comment by: David Engle (1-2-2014 03:10:29)

OMG! Thank you! I have been fighting with the treeview in A2007 for months trying to overcome the Windows 7 problem. Too many workstations to be able to spend that much time. I am adding you to the credits in the About window for the project. Thank You!


Comment by: Antoniu (28-3-2014 16:34:40)

Hello Jan

I am trying to add DblClick for mcTree. I add the event but when I dbl click on node
Private Sub mcTree_DblClick(cNode As clsNode) thas not fire up

(Those are my steps):

go in clsTreeView on TreeControl activate DblClick
Private Sub TreeControl_DblClick(ByVal Cancel As MSForms.ReturnBoolean)

End Sub

put on General - Declaration this line

Event DblClick(cNode As clsNode)

on userform I can see DblClick event

but it is not working

Please can you show me the correct steps? Thank you!


Comment by: Jan Karel Pieterse (28-3-2014 19:07:32)

Hi Antoniu,

You also have to raise the couble_click event using RaiseEvent. You can check how it is done by tracking fdown how we implemented the Click event.

Start from the sub "Private Sub mctlControl_Click()" in clsNode.

In there you will find this line of code:

moTree.NodeClick Control, Me

which calls a routine in the clsTree class:

Friend Sub NodeClick(ByRef oCtl As MSForms.Control, ByRef cNode As clsNode)

In that sub we raise the event so it is exposed to the userform implementing the tree class:

RaiseEvent Click(cNode)


Comment by: Shah (18-10-2014 00:41:33)


I am trying to use the access version of the treeview. I see activatenode as a function in the documentation but not in the downloaded code.

I have set keys for all nodes in my tree. I just want to scroll to the one based on selection in other control. Is there other way to do this?



Comment by: Jan Karel Pieterse (18-10-2014 11:43:52)

Hi Shah,

You can just set the ActiveNode of the tree to the node you need.


Comment by: shah (18-10-2014 14:42:32)

Hi Jan,

I have created a tree with about 16000 nodes with unique keys based on the data that I have in a table. I have created the tree and displayed it in the form.

My desire is to scroll the tree to a specific node based on the key's value. I have not been able to figure out a way to find a node based on the key. So, the right side of the mctree.ActiveNode assignment is where I am having problem.

Hope this clarifies.



Comment by: TPhong (26-11-2014 10:20:22)

Hi there,

I just want to say thank you.
You did a really great job.



Comment by: cris (5-2-2015 00:42:31)


This is great.

I notice some strange behavior with the checkbox button :
if checked before the demo's launched, parent nodes are selected (besides childs). Anyway I could manage to produce the dump of all selected nodes or subnodes.

I'm looking for enhancement to display the nodes that comply with a text search (filter) : we could make successive search and keep "checked" the various checkboxes throughout the different searches. Is there any sample of this ?



Comment by: Jan Karel Pieterse (5-2-2015 06:53:07)

Hi Cris,

I'm afraid there is no sample for that. The checking of parent nodes depends on whether you have tristate set or not.


Comment by: Mathias (23-2-2015 18:02:32)

I like your msforms treeview and i would like to have a treeview without a root because i want all items to be positioned directly under each other (without lines and without Expanders. Could someone help me by usin the code odf the demo-form (cmdgetdata) how this could be solved?

Thank you


Comment by: Jan Karel Pieterse (24-2-2015 06:22:12)

Hi Matthias,

Peter Thornton said:

Hi Mathias,
If I understand your question you only want a single column of nodes, if so
maybe a ListBox or ListView would be more appropriate. However you can add
each node as a root node but with no child nodes and that will create a
column of nodes, adapt Sub InitializeDemo1 in the demo

' demo code
With mcTree
' demo code
    .ShowLines = False ' Me.cbxShowlines.Value
    .Indentation = 0
    For k = 1 To 50    ' # roots
        Set cRoot = .AddRoot(sKey:="Root" & k, vCaption:="My Node" & k)
    ' add or change any cRoot properties as required here
    ' comment all the demo code from here until Next
     'create the node controls and display the tree
End With

Adapt other properties for the treeview as required, eg .FullWidth. Each
(root) node could have a different icon.


Comment by: mathias (24-2-2015 12:20:04)

Hi Peter,
thank you for your prompt answer.

I use the folllwong code to get a treeview with no expanders.

'         Add a Root node with main and expanded icons and make it bold
    Set cRoot = .AddRoot("Root", "", "FolderClosed", "FolderOpen")
        Set dbs = CurrentDb
        ' Open recordset that returns the unique dates from tblBusinessUnit
        strSQL = "SELECT BusinessUnit_ID, BusinessUnit FROM tblBusinessUnit"
        Set rst1 = dbs.OpenRecordset(strSQL, dbOpenForwardOnly)
        ' Loop through the Business Units
        Do While Not rst1.EOF
            ' Add node
            strKey = "B " & rst1!BusinessUnit_ID
            strCaption = "Business unit: " & rst1!BusinessUnit
            Set cNode1 = cRoot.AddChild(sKey:=strKey, vCaption:=strCaption)
            cNode1.Bold = True
            cNode1.Expanded = True

but i don't want a root Checkbox to be shown in the view. (can i load up a Picture here? Just to Show what i mean.)

Got any idea?

Thank you.


Comment by: Jan Karel Pieterse (24-2-2015 12:55:35)

Hi Mathias,

Unfortunately I do not allow any attachments. You could upload one to skydrive and share a link as an alternative however.


Comment by: Mathias (24-2-2015 18:21:25)

next try:

i managed to get a single column of nodes almost loke i wanted to.

I did it like this:

        strSQL = "SELECT BusinessUnit_ID, BusinessUnit FROM tblBusinessUnit"
        Set rst1 = dbs.OpenRecordset(strSQL, dbOpenForwardOnly)
        ' Loop through the Business Units
        Do While Not rst1.EOF
            ' Add root
            strKey = "B " & rst1!BusinessUnit_ID
            strCaption = "Business unit: " & rst1!BusinessUnit
            Set cRoot = .AddRoot(sKey:=strKey, vCaption:=strCaption)
            ' Open recordset that returns the Main Categories for each BusinessUnit
            strSQL = "SELECT Zwischen_ID, MainCategories FROM tblZwischenBusiness WHERE BusinessUnit_ID=" & _
            rst1!BusinessUnit_ID & " ORDER BY MainCategories_ID"
            Set rst2 = dbs.OpenRecordset(strSQL, dbOpenForwardOnly)
            ' Loop through the maincategories
            Do While Not rst2.EOF
                 'Add Node
                strKey = "M" & rst2!Zwischen_ID
                strCaption = "Main Categorie: " & rst2!Maincategories
                Set cNode2 = .AddRoot(sKey:=strKey, vCaption:=strCaption)
                ' Open recordset that returns the students for each class
                strSQL = "SELECT ZwischenSub_ID, Subcategories FROM tblZwischenSub WHERE Zwischen_ID=" & _
                rst2!Zwischen_ID & " ORDER BY ZwischenSub_ID"
                Set rst3 = dbs.OpenRecordset(strSQL, dbOpenForwardOnly)
                 'Loop through the subcategories
                Do While Not rst3.EOF
                cNode2.Expanded = True


Comment by: Peter Thornton (25-2-2015 15:19:32)

Mathias, it's hard to recreate your code but it looks like you are adding a single column of root nodes as I suggested. Keep in mind Expanders and ShowLines are true by default, though not Checkboxes. If your cNode2 does not have any child nodes there is no need to do cNode2.Expanded = True (actually Expanded = True is default)

You say you've got it working almost as you want, in what way is it not quite as you want.


Comment by: Mathias (26-2-2015 10:05:00)

Hi Peter,

in my code example: how can i get a node being underlined?.



does not work.




Comment by: Jan Karel Pieterse (26-2-2015 10:08:18)


Please end a piece of code with [/vb] instead of [vb]


Comment by: Jan Karel Pieterse (26-2-2015 10:10:00)


Please end a piece of code with square bracket /vb square bracket (so include the slash)


Comment by: Peter Thornton (26-2-2015 12:43:10)

Mathias, we didn't include a Node.Underline property but you can add it yourself. Search both classes for "Bold": if you don't use Bold simply Replace most* of the "Bold" with "Underline", otherwise copy* the Bold code* in both classes and adapt to Underline.

*note Bold is also used in Expanders, don't change or copy that near where you see "-" and "+" (only in clsTreeView).

Add comments so you can easily adapt the next version.


Comment by: Ian (4-4-2015 10:42:46)

How do I return a collection of a nodes children. The nodes.children property seems to return nothing


Comment by: Jan Karel Pieterse (5-4-2015 15:06:05)

Hi Ian,

You need the ChildNodes collection.


Comment by: Paco (6-5-2015 15:40:32)

This is an amazing piece of ingenuity! Elegant, unpretentious, very clever. Thanks for sharing.

Would you release drag & drop functionality anytime soon?

Any chance you could consider multi-select?

Kindest regards!


Comment by: Jan Karel Pieterse (6-5-2015 16:20:47)

Hi Paco,

Thank you!

Multi-select. Hmm. You can by enabling check boxes. You can check as many boxes as you like.


Comment by: Elissa (15-5-2015 02:33:01)

Looks fantastic!
2 Questions:
1. Can it read from tables and alter the icon based on the data in the table?
2. Can it then be clickable, so a form pulls up based on which record is clicked?


Comment by: Peter Thornton (18-5-2015 11:35:49)

Hi Elissa,
You can trap events to respond to the newly selected node, then pass details of the node to your own routine that reads your table to return details about the applicable icon.

Since build 026 icons can be updated to nodes during run-time, see Treeview.ImageAdd and Node.ImageUpdate in the documentation (in the Excel demo). Also see the examples in the demo form to respond to mouse and keyboard events.


Comment by: Tom (15-9-2015 11:37:09)

Hi gentleman,

I'm trying to unload / change icons during runtime, but this does not seem to work?

Let's say I create a treeview, based on the with Icons example
Initially the treeview nodes don't have Images (only root has default Image)

Then I'm processing some data and based on some assertion
In case not ok: cNode.ImageMain = "nok"
In case ok: cNode.ImageMain = "ok"
mcTree.Refresh to rebuild
This seems to work

Now the issue 1:
When 'resetting' I'd like to have No icons, so I run for root.Children a loop: cNode.ImageMain = ""
But the Images still there

Now the issue 1:
When 'changing' I'd like to have all to "nok" icons, so I run for root.Children a loop: cNode.ImageMain = "nok"
But the Images still there (the old asserted "ok")

So, how can I update without assining a new collection to the treeview?

Thanks for your help!



Comment by: Peter Thornton (22-9-2015 12:57:28)

Tom, sorry for late reply only just seen your comment.

The option to change icons during runtime was added in build 026 with ImageAdd and ImageUpdate (see the documentation). However we didn't consider anyone would want to remove icons from individual nodes altogether!

Contact me privately and I'll send you a small fix which hopefully will meet your needs.


Comment by: Jon von der heyden (21-10-2015 11:03:07)

Using this for the first time and loving it! That is all. :)


Comment by: Jan Karel Pieterse (21-10-2015 15:18:05)

Hi Jon,

Glad you like it!


Comment by: Frank (20-1-2016 14:13:37)

Hi there - thank you for this great component. With this I could replace the annoying activex treeview.

I have one question - I would like to open a data record according to which Root/node was clicked. As in root I have jobs and in the nodes I have services contained in the jobs I must know what was clicked (root or node) and maybe read the key from the root/node. Is that possible? Thanks for help!


Comment by: Jan Karel Pieterse (20-1-2016 15:05:44)

Hi Frank,

If you implement the mcTree_Click event, the argument you get passed to you is the node you clicked:

Private Sub mcTree_Click(cNode As clsNode)
    Dim cChild as clsNode
    If Not cNode.ChildNodes Is Nothing Then
        For Each cChild In cNode.ChildNodes
            'Now do something with the childnode.
    End If
End Sub


Comment by: swapnil bhalerao (30-1-2017 12:14:31)

I am working on similar tree view but I want multiple checkboxes for each node (at least three) each representing cell of three different columns, also if I checked 3-4 nodes it must show value from an associated cell from the columns for which particular checkbox has been checked.

C1 C2 C3 are Checkboxes for each node if I checked C1.1(Node1) & c1.2(Node2) also C1.3(Node3) (First checkbox of three different nodes) it should show the sum value ( First checkbox C1 is associated with column 1) after cmd button has been hit.


Comment by: Peter Thornton (31-1-2017 17:55:07)

Hi bhalerao,
It would be possible to add multiple checkboxes for but far to much to show how here. Try and follow how icons and checkboxes are added, and increase the offsets to add to Nodes' Left property. Event handling in the node class, and referred to the main treeview class can be added similar to the existing single checkbox.

I don't really follow the second part of your question, but once your event handlers are in place you can include code in the main form's event handlers to apply the combinations you require.


Comment by: Pablo (15-2-2017 21:49:17)

Hi, This is an awesome component, but I'm experiencing something really odd, when I try to add event handlers to a node click event I got the event is raised even when a key is pressed (e.g.: Enter, Arrow Up, etc)
Do you have any suggestions on how to solve this?

P/S: I'm working on an Access 2013 Database. I see the same behavior on your sample app as well.


Comment by: Peter Thornton (16-2-2017 13:37:11)

Hi Pablo,
Indeed, the navigation keys will indirectly raise the click event, which means you can trap a node has been activated whether my mouse or keyboard. This 'by design' and consistent with equivalent app's.

If you want disable keyboard navigation comment the relevant key actions in TreeControl_KeyUp and _KeyDown.

If you want to allow keyboard navigation but not raise any click events, you would need to adapt quite a bit more. But are you really sure you want to do that!


Comment by: Pablo (17-2-2017 21:33:43)

Hi Peter,
Thank you for your quick answer, indeed I'd like to use keys for navigation and only raise a click event only when pressed "Enter" rather than on every keystroke, I'm trying to use the TV as a menu, where you can click or press enter to open the desired form. I tried also trapping the _KeyUp event but for some (odd) reason the keycode for "Enter" is the same as the left-arrow.
I'd be grateful if you can point out where to make the changes you mentioned in this line: "allow keyboard navigation but not raise any click events"



Comment by: Peter Thornton (18-2-2017 11:06:05)

Hi Pablo, OK I think I follow.

In TreeControl_KeyDown replace 3 instances of "Caption", 1 with 0
In the same event near the top, after "If KeyCode = vbKeyReturn" comment the If/Else/End if and replace with:
If Not moActiveNode Is Nothing Then
    RaiseEvent Click(moActiveNode) ' Enter will raise the click event
End If

Or, trap vbKeyReturn in the mcTree_KeyDown or KeyUp event in the main form.

FYI Enter in other treeviews expands or collapses a branch, like the left/right arrow keys, and explains the "odd" reason Enter morphed into an arrow!


Comment by: Ray Buckley (19-10-2017 12:31:03)

Are you supporting drag drop functionality yet


Comment by: Peter Thornton (19-10-2017 17:16:41)

Hi Ray,

Drag & drop is supported the 'pro' (see above) but it's not viable in the free version, at least not in the way we'd want to do it.


Comment by: Robin Ball (14-3-2018 16:34:35)

I am working on a Bill of Materials system and have a working recursive SQL server script that populates a single table with all components and sub builds and their components and their level number in the hierarchy.

Your free Treeview control looks excellent and I could organise my data easily enough to replicate your multi-table approach but in doing so will lose the benefits of my recursive code. I could code to choose how many levels to display for any specific parent item and its multi-level children to an arbitrary number of levels (say 12) which is currently deeper than real world data.

2 questions

Is there a depth limit to the number of levels in the hierarchy ?

Does the pro version allow for recursion and an unknown number of levels ?


Comment by: Jan Karel Pieterse (14-3-2018 17:33:16)

Hi Robin,

Theoretically there are no depth limits. But I would limit use of our free treeview to a max of about 1,000 nodes to get good performance. The pro version can handle thousands of nodes without issue.
Recursively adding nodes should be easy enough but is left as an excersize to the reader :-)
There is a post here that has some code, perhaps that helps:


Comment by: Bert (14-5-2018 14:11:43)


This is a very nice treeview control.

I do run in one issue though. I'm adding childnodes by selecting the parent node and then using the .Addchild method. That works well until it can't find the parent node. Then I get an VBA error 5:Invalid procedure call or argument.
In this case I just want to ignore this, but how can I do this? I tried several things like:

Set cNode = .Nodes(rst!Parent)
if Not cNode is Nothing then
cChild = cNode.AddChild(sKey:=strKey, vCaption:=strCaption)
end if

But no matter what I try, I keep getting error 5 on line:

Set cNode = .Nodes(rst!Parent)

How can I check if a node exists before I try to use it?


Comment by: Jan Karel Pieterse (14-5-2018 15:46:57)

The nodes are only accessible through their index, not through their name so you would have to enumerate the nodes and then check their Name to find the right node. Air code:

Function GetNode(sName As String)
    Dim oNode as Node
    For Each oNode in mcTree.Nodes
        If oNode.Caption = sName Then 'Or use .Key
            Set GetNode = oNode
            Exit for
        End If
End Function


Comment by: Jim Dettman (28-1-2019 15:25:02)

The features page shows a ActivateNode method, yet I cannot locate this in the Access or Excel this a feature of the "Pro" version?



Comment by: Peter Thornton (29-1-2019 09:49:45)

Hi Jim,

Indeed the 'Pro' has many additional features but the ActivateNode method is also in the 'free' Access and Excel versions. If you look in the top right drop-down in the clsTreeView module it's the first method listed.


Comment by: Jim Dettman (29-1-2019 12:42:59)

Thanks for responding so quickly, but unless I have the wrong download (I have build 026, which is a .MDB file), I don't see any ActivateNode method. There is an ActiveNode property listed first, and the first method shown for the class is AddNodeToCol.

I'm also the second person that could not find this. A question was posted on Experts-Exchange from someone trying to use the control:

However after posting my last comment here, I found a similar question on the page here along with the required code, so we are set.    

But you might want to check your current build. There is no need to publish this comment unless you wish to do so.



Comment by: Jan Karel Pieterse (2-2-2019 16:14:33)

Hi Jim,

The docs are wrong, simply use the Activenode property as it is read/write so you can change the activenode simply by using code like:

Set mcTree.ActiveNode = cNode


Comment by: Akira HOSOKAWA (31-7-2019 10:39:00)

This TreeView control works perfectly on my VBA project.

Is there a way to get an image data (via clipboard or as an image file of JPG or PNG) of a whole tree structure which is displayed on the TreeView control?

Thanks in advance.


Comment by: Peter Thornton (31-7-2019 15:30:00)

Hi Akira,

It's easy to copy the form with Alt - Print-Screen, but I don't know how to capture the non-visible area scrolled off the form (or off the screen) which I guess is what you need.

Have you tried the 'Dump Data' example on the demo form which copies the Treeview data and structure to a worksheet. If you format cells as text you can resize column widths so nodes at different levels overlap slightly, more like the treeview.


Comment by: Andrew de beer (30-9-2019 11:19:00)

Is there a "word wrap" property for a treeview node?


Comment by: Peter Thornton (30-9-2019 16:37:00)

Hi Andrew,

There is isn't a "word wrap" property, complicated as it can be arbitrary when and where 'virtual' line breaks are added.

Nodes can handle multi-line (line breaks). However you would also need to size 'nodeheight' to at least the height as the node with most lines (handled automatically in the pro treeview).


Have a question, comment or suggestion? Then please use this form.

If your question is not directly related to this web page, but rather a more general "How do I do this" Excel question, then I advise you to ask your question here:

Please enter your name (required):

Your e-mail address (optional, will only be used to inform you when your comment is published or to respond to your question directly):

Your request or comment (max 2000 characters):

To post VBA code in your comment, use [VB] tags, like this: [VB]Code goes here[/VB].

I give permission to process this data and display my name and my comment on this website accoring to our Privacy Policy.