Most Valuable Professional

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

Audit !!!

Check out our RefTreeAnalyser
the ultimate Excel formula auditing tool.

Speed up your file

The best tool to optimise your Excel model!
Home > English site > Articles > Treeview control

An MSForms (all VBA) treeview


If you have ever used the Treeview control from the "Additional controls" section, then you know what a versatile control this is to show hierarchically organized data. There are a couple of problems with this Treeview control:

  1. Compile errors due to a difference in how the control libraries are registered in 32 bits Windows' System32 and 64 bit Windows' SysWOW32 folders. If you distribute a file that was saved in 64 bit Windows, containing one of the "Microsoft Windows Common Controls 6.0" (The Treeview control is one of them) and with the reference set to "mscomctl.ocx", people using 32 bit Windows will almost certainly have problems. At best it could entail removing both the control and the reference and replacing both, but at worst the user's Excel can crash when trying to load the file and run the app.
  2. The standard Treeview control, like all non built-in ActiveX controls, cannot be used in 64 bit versions of Office.

Especially the second point convinced me it is time to develop a custom-made Treeview "control", that only uses the native Office forms controls. I started building this a couple of weeks ago and after some time I tricked Peter Thornton into helping me with it

The screenshot below shows both our new Treeview (left) and the Windows one (right) side-by-side in their simplest display mode (read on, there are even prettier screenshots further down the page):

Treeview controls
Two treeviews, left: VBA tree, right: Common controls tree

In the following pages I'll show what our treeview can do and explain how to put it to use in your own VBA project.

MAC Office and 64 bit Office Compatible!

Our treeview was tested (and works) on 64 bit Office. It also works on MAC office. Tested applications include:

Excel 2000
Excel 2003
Excel 2010
Excel 2010 (64 bit)
Excel 2011 (MAC Office)
Excel 2013 (32 bit)

Access 2003
Access 2010 (32 bit)
Access 2010 (64 bit)

Windows screenshot:

Treeview demo on Windows Excel
Treeview demo on Windows Excel

Mac screenshot:

MAC screenshot of our treeview demo form
Screenshot of treeview on Mac Excel 2011


The basic plumbing and code structure of this treeview control was devised by me. However, without the help of my friend and fellow MVP Peter Thornton, lots of functionality would not have been available now. For that I sincerely thank Peter!

Furthermore, Access MVP Ben Clothier was kind enough to make the necessary adjustments to incorporate the treeview in an Access form

Also: Fellow Excel MVP Ron De Bruin ensured the treeview also works on MAC Office 2011, Thanks Ron!

Copyright and Licensing

All code in the treeview is (c) JKP Application Development Services and Peter Thornton (the Authors). It remains our sole intellectual property.

However, we're offering this treeview to you at no cost. You get an unrestricted license for use in any VBA project you like. You're free to modify any part of the code at will.

We do have some rules:

We're always interested to see how people have implemented the VBA Treeview. So please feel free to send a screenshot with a brief description or relevant details.


You use this control at your own risk: The authors accept no liability whatsoever for any damages which may arise due to the use of our treeview.


Many, many hours were spent developing this treeview. Although we developed it for use in our own projects, we are giving it away for free!

Nevertheless, we would really be pleased if you actually express your appreciation in a more "tangible" form. So here is a paypal donation button at your disposal:


The Excel workbook contains most of the documentation (on the tabs of the workbook), so I recommend you to at least download the Excel version. The Access version has instructions on its main form on how to implement the treeview in your own projects.

Download the treeview sample Excel workbook (including documentation) (build 025, 16 Oct 2013, downloaded 9854 times)

Download the treeview sample Word document (build 025, 16 Oct 2013, downloaded 2552 times)

Download the treeview sample Access database (build 025, 16 Oct 2013, downloaded 8882 times)



Other controls

Another often used control is the calendar control. This control has the added problem that it has been deprecated with Office 2010 (where we're supposed to use the date picker control). Frankens Team created an all-vba alternative using very similar techniques to what we've done here.

Ron de Bruin created a Date Picker control for MAC Excel.


Showing last 8 comments of 285 in total (Show All Comments):


Comment by: Dale Fye (10/21/2014 2:09:37 PM)

Have been playing with the treeview for several days now. Was able to add code on the MouseUp event to display a popup menu, that worked great. One of the items on my popup menu is to expand or collapse the currently selected node and all of its children.

The default, when you collapse the root node appears to collapse that node, but to leave all other expanded nodes expanded. Since my tree can 4 or 5 levels deep, I want to be able to right click on the node and select Expand All or Collapse all. Right now, my Collapse code looks similar to:

set nd = forms("frmMain).mctree.activenode
IF CommandBars.ActionControl.Parameter = "Collapse" Then
    CollapseBranch nd
    ExpandBranch nd

Sub CollapseBranch(cNode As clsNode)

    Dim cChild As clsNode
    cNode.Expanded = False
    If Not cNode.ChildNodes Is Nothing Then
        For Each cChild In cNode.ChildNodes
            CollapseBranch cChild
    End If
End Sub
Sub ExpandBranch(cNode As clsNode)

    Dim cChild As clsNode
    cNode.Expanded = True
    If Not cNode.ChildNodes Is Nothing Then
        For Each cChild In cNode.ChildNodes
            ExpandBranch cChild
    End If
End Sub

But all this is doing is changing the icon in front of the node from - to + (or + to -), it does not actually collapse or expand the node.


Comment by: Dale Fye (10/21/2014 2:51:30 PM)

Is there a way to implement the MouseUp event in the main form? I notice in the frmDemo that there are Click, KeyDown, and NodeCheck events listed, but don't see the MouseUp event.

I've added code to the mctlControl_MouseUp subroutine in clsNode, but would like to leave that code unchanged and simply be able to refer to the mcTree_MouseUp event in my main forms code module.

Is there something special that I need to do to make that event visible in the main form?


Comment by: Jan Karel Pieterse (10/21/2014 3:30:58 PM)

Hi Dale,

The easiest way is to call the mcTree.NodeClick method with the Node's Expander control as the first argument. Like so:

        nd.Expanded = Not nd.Expanded
        mcTree.NodeClick nd.Expander, nd


Comment by: Dale Fye (10/21/2014 4:10:34 PM)

That seems to have done it. Thanks.


Comment by: Dale Fye (10/22/2014 4:05:38 PM)


I'm trying to add some images to the image frImages in Access 2007. I was able to drill down to the image control on the form, and then select properties, but am unable to change the actual image.

I noticed a thread above that you cannot currently edit the images in Access, but I'm the first to admit that I'm an idiot when it comes to working with Excel UserForms.

I added a user form to an Excel file, added an image control to it, but cannot figure out how to actually assign an image (bmp, png, ico) to that image control.


Comment by: Jan Karel Pieterse (10/23/2014 7:07:56 AM)

Hi Dale,

There are various ways to add an image to an image control on a VBA Userform:

- At runtime you can use the LoadPicture method
- At design time:
Copy the picture and paste it into the picture property of the image control
Click the three-dotted button next to the image property and select an image file


Comment by: Isabella Tsoglin (10/27/2014 2:25:49 PM)

Thanks for your amazing work.
Works good with Access 2010.
I'll try to add "dblclick" event, not yet done.


Comment by: Jan Karel Pieterse (10/27/2014 2:46:29 PM)

Hi Isabella,

You're welcome!


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 but if you want me to respond it helps!; will not be shown, nor be used to send you unsolicited information):

Your request or comment:

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