Excel: An msforms (all VBA) treeview; How to use http://www.jkp-ads.com/Articles/treeview01.
asp
WWW This site
for Microsoft Office
Home > English site > Articles > Treeview control > How To Use
Subscribe to our
An MSForms (All VBA) mailing list
Treeview; How To Use * indicates required
Email Address *
Home This page outlines the minimum steps
News needed to add this treeview control to your First Name
own Excel or Word VBA project. For Access
Newsletter
the instructions are different, those can be Last Name
Events found in the Access download.
Headlines
Products The container control Audit !!!
Excel Training Open the designer window of your userform Check out our
RefTreeAnalyser
Recommendations and add a frame. This is where the treeview
the ultimate Excel formula
Services will be built. We recommend these auditing tool.
properties (which are of course optional):
Articles Trainings
Addin Installation Excel VBA Masterclass
Add-ins do not load (English)
API declarations Excel VBA for Financials
Catch Paste
(Dutch)
Build Excel Add-in
Third party tools
Chart an Equation
Circular References Speed up your file
Control Events
FastExcel
Controls
The best tool to optimise
Corrupt Files your Excel model!
Create Addins
Custom Find Repair your file
Excel data table
recipe Stellar Phoenix Excel
Repair
Excel Web App
Best tool to repair corrupt
Mashups
Excel sheets and objects
Defined Names
Disable Events
Docking VBE Windows
Excel 2007 FileFormat
Excel Tables
Excel Tables (VBA)
Fix Links to UDFs
Formula Wrapper
Import textfiles
Inventory System
Keep Userform On Top Recommended properties for the container frame
Listbox AutoSize
Least Squares Your userform would look like this:
Object Lister
1 of 10 4/Jun-17 00:36
Excel: An msforms (all VBA) treeview; How to use http://www.jkp-ads.com/Articles/treeview01.asp
Pivottable Slicers
Performance Class
Prevent Open Event
Register UDFs
Round2Digits
Select a range (VBA)
Show Picture
Startup Problems
Styles in Excel
Transpose Table
Treeview control
Features
How To Use
Examples
Undo With Excel VBA
Update An Addin
WebQuery
Wheel of Fortune
The container frame on your userform
Workbook Open Bug
XML and Excel If you want images in your treeview, you'll
have to add another frame (we called it
Books
frmImageBox) and then add Image controls
Reviews within that frame. Set the Visible property of
Downloads the frame to false to avoid it showing up on
Links your userform. Like so:
Contact
Site map
Subscribe in a reader
The images frame on your userform
If you want to add images, make sure you
name the new image controls properly, as it
is the name of the imagecontrol you need to
pass to the cNode class to get that image
displayed. An easy way to get the images is
to copy the entire frame with images from
our userform to yours.
Class modules
2 of 10 4/Jun-17 00:36
Excel: An msforms (all VBA) treeview; How to use http://www.jkp-ads.com/Articles/treeview01.asp
Copy the class modules clsNode and
clsTreeView to your project (you can simply
drag them to your project). Your project (if
you started with an empty workbook) would
look like this:
The project with class modules in place
So far, things have been simple. The next
parts are a bit more compex, but nothing too
hard!
Code behind your userform
For the treeview to work, your userform
needs some code, in particular these
elements are necessary:
Some variable declarations that will
enable you to address the treeview
and have your form handle events
Initialisation code to:
add nodes to the tree
draw the treeview
That's it!
Variable declaration
Add this code to the declaration section of
your userform:
'Add this to your form's declaration
section
Private WithEvents mcTree As clsTreeView
That's it! No more variables needed!
Of course the name of the variable mcTree is
totally up to you.
If you need another (independent) treeview
on your form, simply add another frame to
hold it and an additional variable in the
forms declaration section (of course you
name it differently than the other one) like
the one shown here.
Initialisation
In the intialisation routine of your form, you
need code that adds nodes to the tree and
when you're done adding nodes, you need to
3 of 10 4/Jun-17 00:36
Excel: An msforms (all VBA) treeview; How to use http://www.jkp-ads.com/Articles/treeview01.asp
set some properties of the treeview. Then
you'll want the treeview to be displayed.
Adding an instance of the treeview to
your form
Adding the instance is easy:
'Instantiate a new instance of the
treeview class and set a module level
variable to hold it:
Set mcTree = New clsTreeView
Now tell the tree class instance which frame
is its container:
With mcTree
'The VBA treeview needs a
container Frame control on the userform.
'Just draw a frame on your form
where the treeview should appear
'Make sure the frame has no
border and no caption
'Set it to show both scroll bars.
(Keepscrollbarsvisible to both)
'Set it to the special effect
"Sunken"
'Note that node label formats
such as back/fore colors and font adopt
'the frame container's
properties, so change if/as required.
'(you can copy the frame named
frmDemo from this userform )
'Then pass this frame to the
TreeControl of the treeview class
Set .TreeControl = Me.frmDemo
'Title for message boxes:
.AppName = Me.AppName
Note that most of the code listed
below is within the With mcTree ...
End With structure.
Setting initial look
You'll want control over the look and feel of
your treeview, here is some example code
(this code comes immediately below the
code sample show above):
'Set some properties
.CheckBoxes = True
.RootButton = True
.LabelEdit = 0 'default is 0 can
be edited (like LabelEditConstants
tvwAutomatic/tvwManual)
.Indentation = 20 * 0.75
'defaults to 11.25
.NodeHeight = 16 * 0.75 'defaults
to 12
.ShowLines = True
'If your form has icons in an
iconframe (called frmImageBox),
'you could use icons for the
expand and collapse buttons:
Call
.ExpanderImage(Me.frmImageBox.Controls("Win7Minus").Picture,
Me.frmImageBox.Controls("Win7Plus1").Picture)
4 of 10 4/Jun-17 00:36
Excel: An msforms (all VBA) treeview; How to use http://www.jkp-ads.com/Articles/treeview01.asp
If your treeview needs to show images, add
a frame control with Image controls inside.
Lets call it frmImageBox. This is how you tell
the class where the images are:
Set .Images = Me.frmImageBox
That is just about all the plumbing you need
to get started.
Adding nodes
First of all, a couple of variables are needed
to add nodes:
'The root of the tree
Dim cRoot As clsNode
'A node
Dim cNode As clsNode
'An extra variable should you need to
remember a certain node
Dim cExtraNode As clsNode
Next we'll start by building the rootnode:
' add a Root node with main and
expanded icons and make it bold
Set cRoot = .AddRoot("Root",
"Root Node", "FolderClosed",
"FolderOpen")
cRoot.Bold = True
Note that the tree can have more than one
rootnode, there is a special RootNodes
collection to which you automatically add
new roots by calling the AddRoot method.
As you can see, we assume there are two
icons in the image frame called FolderClosed
and FolderOpen respectively.
Now we want to add children to the root.
This is the code from our demo form:
'Add branches with child nodes to
the root:
'Keys are optional but if using
them they must be unique,
'attempting to add a node with a
duplicate key will cause a runtime error.
'(below we will include unique
keys with all the nodes)
Set cNode = cRoot.AddChild("1",
"1 A", "FLGNETH")
cNode.Bold = True
'Add a 2nd branch to the root:
Set cNode = cRoot.AddChild("2",
"2 B", "FLGSWED")
cNode.Bold = True
'If you want to add more child
branches to a branch later on, use a
variable to store the branch.
Set cExtraNode =
cNode.AddChild("2.1", "2.1 level 2",
"NOTE03", "NOTE04") ' include an
expanded icon
cExtraNode.Expanded = False '
this node will initially be collapsed,
'
its child node controls will be created
when expanded
5 of 10 4/Jun-17 00:36
Excel: An msforms (all VBA) treeview; How to use http://www.jkp-ads.com/Articles/treeview01.asp
'To add a branches to a branch,
make sure you set a variable to its
'main' or parent branch when created
'Then use the Branch's AddChild
method, here to create multiple levels
Set cNode = cNode.AddChild("2.2",
"2.2 level 2", "NOTE03", "NOTE04") '
include an expanded icon
Set cNode =
cNode.AddChild("2.2.1", "2.2.1 level 3",
"OpenBook")
Set cNode =
cNode.AddChild("2.2.1.1", "2.2.1.1 level
4", "Scroll")
Set cNode =
cNode.AddChild("2.2.1.1.1 ", "2.2.1.1.1
level 5", "GreenTick")
'Now add another branch to the
branch we stored earlier
cExtraNode.AddChild "2.1.1",
"2.1.1 level 3", "OpenBook"
'Add a 3rd branch to the root,
with a child node
Set cNode = cRoot.AddChild("3",
"3 C", "FLGUK")
cNode.Bold = True
cNode.AddChild "3.1", "3.1 level
2", "Scroll"
' and add a 4th branch to the
root
Set cNode = cRoot.AddChild("4",
"4 D", "FLGUSA02")
cNode.Bold = True
cNode.Caption = "4 D +" &
mlCntChildren
' add a bunch of child nodes to
the 4th branch
For i = 1 To mlCntChildren ' 15
Set cExtraNode =
cNode.AddChild("4." & i, " 4.1 " &
Right$("000" & i, 4), "Scroll")
' add some alternate row
colour formats
If i Mod 2 Then
cExtraNode.BackColor =
RGB(255, 255, 220) ' pale yellow
cExtraNode.ForeColor =
RGB(180, 0, 0) ' dark red font
End If
Next
Display the tree
Displaying the tree is as simple as calling
one method:
'Fill the tree
.Refresh
Termination
When the form goes out of scope (i.e. out of
memory) you need to remove the treeview
from memory:
Private Sub UserForm_QueryClose(Cancel As
Integer, CloseMode As Integer)
'Make sure all objects are destroyed
If Not mcTree Is Nothing Then
mcTree.TerminateTree
End If
6 of 10 4/Jun-17 00:36
Excel: An msforms (all VBA) treeview; How to use http://www.jkp-ads.com/Articles/treeview01.asp
End Sub
Feedback
We've worked hard to create a reliable and
performant treeview. If you encounter bugs,
please let us know so we can work on them.
Better yet: if you have fixed a bug you
found, send us your updated code so we can
add the fixes you made.
In any case, comments (and compliments)
are welcome!
Prev: Features Next: Examples
Comments
Showing last 8 comments of 195 in total
(Show All Comments):
Comment by: Zoltan Attila (3/14/2017
3:04:28 PM)
deeplink to this comment
Hello there,
first of all I want to thank you sharing your
wunderfull treeview.
I would like to enable the doubleclick and
the mouseup events but I'm not able to do
it... Could you please help me?
By double click event I want to change the
Icons and add some comments to the Nodes
By mouse up event I would like to
implement a dynamic popup menu in the
treeview depending if I right click on the root
or on a node.
Thank you in advance for your help.
Comment by: Jan Karel Pieterse (3/14/2017
4:27:57 PM)
deeplink to this comment
Hi Zoltan,
Have a look at this comment, perhaps it
helps?
7 of 10 4/Jun-17 00:36
Excel: An msforms (all VBA) treeview; How to use http://www.jkp-ads.com/Articles/treeview01.asp
http://www.jkp-ads.com/articles
/treeview01.asp?AllComments=True#19932
Comment by: Roberto (3/30/2017 11:48:45
PM)
deeplink to this comment
I have a problem to change the picture into
Forms.Frame.1 class
I choose picture property for XPMinus, after
the program open the folder for select the
image and save all, the showing drawing non
change.
what can I do ?
many thank's
Comment by: Peter Thornton (4/1/2017
10:20:15 AM)
deeplink to this comment
Hi Roberto,
In Access it can be difficult to add or change
the 'picture' property in Image controls on
the Frame.
Simple way is to Cut the Image control from
the Access Frame, and paste it into a frame
or even directly onto a userform in Excel;
add or change the the 'picture' property in
Excel, then copy the Image control back to
the Access Frame.
You can create all your Images in Excel, then
copy all in one go to the Frame in Access.
Comment by: filou07 (5/12/2017 11:15:17
AM)
deeplink to this comment
How can I count all children and sub-sub-
sub...children of a node ?
Comment by: Ashish (5/12/2017 11:24:48
AM)
deeplink to this comment
Hello everyone,
Is this possible to add multiple columns in
8 of 10 4/Jun-17 00:36
Excel: An msforms (all VBA) treeview; How to use http://www.jkp-ads.com/Articles/treeview01.asp
tree view.
Comment by: Peter Thornton (5/13/2017
5:08:09 PM)
deeplink to this comment
Hi Ashish,
It is possible but means significantly
adapting the treeview code. I've seen
various attempts but none that appear to
work very well. Better to display columns of
data in an adjacent listbox/listview, or our
ListGrid. Look at Windows Explorer, with a
treeview in the left panel and list on the
right.
Comment by: Peter Thornton (5/13/2017
5:14:38 PM)
deeplink to this comment
@ filour07
Try this recursive routine to count all child
and grandchild nodes
Sub recChildNodesCount(cNode As clsNode,
count As Long)
Dim cChild As clsNode
If Not cNode.ChildNodes Is Nothing
Then
For Each cChild In
cNode.ChildNodes
count = count + 1
recChildNodesCount cChild,
count
Next
End If
End Sub
Call it like this
Dim count As Long
recChildNodesCount mcTree.ActiveNode,
count
MsgBox count
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:
www.eileenslounge.com.
Please enter your name (required):
9 of 10 4/Jun-17 00:36
Excel: An msforms (all VBA) treeview; How to use http://www.jkp-ads.com/Articles/treeview01.asp
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].
Copyright 2017, All Rights Reserved.
10 of 10 4/Jun-17 00:36