ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Leafnode and key in TreeView? (https://www.excelbanter.com/excel-programming/385908-leafnode-key-treeview.html)

JimAnAmateur

Leafnode and key in TreeView?
 
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


Chip Pearson

Leafnode and key in TreeView?
 
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)


"JimAnAmateur" wrote in message
...
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




JimAnAmateur[_2_]

Leafnode and key in TreeView?
 
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

"Chip Pearson" skrev i melding
...
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)


"JimAnAmateur" wrote in message
...
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






JimAnAmateur[_2_]

Leafnode and key in TreeView?
 
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

"JimAnAmateur" skrev i melding
...
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

"Chip Pearson" skrev i melding
...
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)


"JimAnAmateur" wrote in message
...
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









All times are GMT +1. The time now is 02:39 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com