Leafnode and key in TreeView?

G

Guest

I am working on a macro to build/use a TreeView for
structuring/viewing/editing
the content in an Excel file (Excel 2003).

Two questions I hope you experts out there may help me with:
1) How to determine if a node is a leafnode?
2) How to determine if a key is not already in use and thus unique (new
node)?

Jim
 
C

Chip Pearson

Jim,

If by "leafnode" you mean a node with no children, use something like the
following. The second half of the code will test if a key is in use.

Dim Nd As MSComctlLib.Node
Dim KeyName As String
On Error Resume Next
''''''''''''''''''''''''''''''
' Test if SelectedItem has
' at least one child.
''''''''''''''''''''''''''''''
With Me.TreeView1.SelectedItem
Err.Clear
Set Nd = .Child
If Not Nd Is Nothing Then
MsgBox "Item: " & .Text & " has at least one child."
Else
MsgBox "Item: " & .Text & " has no children"
End If
End With


''''''''''''''''''''''''''''''
' See if KeyName exists.
''''''''''''''''''''''''''''''
KeyName = "TestKeyName"
With Me.TreeView1
Set Nd = Nothing
Set Nd = .Nodes(KeyName)
If Nd Is Nothing Then
MsgBox "Key not found"
Else
MsgBox "Key found"
End If
End With



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)
 
J

JimAnAmateur

Thanks Chip!

1) Leafnode: It works now!

2) Unique key: Not yet, I got "run-time error '35601': Element not fount" at
the statement "Set Nd = .Nodes(KeyName)"

Please, more advice!

br Jim
 
J

JimAnAmateur

Sorry, my fault :-(
I don't know/remember how and why, but I in one way or another missed the
statement "On Error Resume Next".

Your sample code for testing if KeyName exists works!

Thanks again, Chip!

br Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top