Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Treeview - Expand all parents of node

Hi All

I have a userform with a treeview of an account hierachy. What l am
trying to achieve with the code snippet below is find a node (in this
case with Key = "ADMIN PAY") and then expand all the parent nodes. The
code below works in that it finds the target node and expands that and
the immediate parent but does not go to the next level.

A further complication is that the node to be found can be at any
level between 2 to 7 and all parent nodes above that should be
expanded.

Any help to overcome this problem will be graetly appreciated


Private Sub CommandButton2_Click()

Dim branch As Variant
Dim ToFind As Variant

ToFind = "ADMIN PAY"

With UserForm1.TreeView1
For Each branch In .Nodes
If branch.Key = ToFind Then
branch.Expanded = True
branch.Parent.Expanded = True
End If
Next
End With

End Sub

Regards

Michael
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Treeview - Expand all parents of node

Does this work

Private Sub CommandButton2_Click()

Dim branch As Variant
Dim ToFind As Variant

ToFind = "ADMIN PAY"

With UserForm1.TreeView1
For Each branch In .Nodes
If branch.Key = ToFind Then
branch.Expanded = True
Set NewBranch = Branch
Do while not NewBranch.Parent is nothing
Set NewBranch = NewBranch.Parent
NewBranch.Expanded = True
Loop
End If
Next
End With

End Sub

"michael.beckinsale" wrote:

Hi All

I have a userform with a treeview of an account hierachy. What l am
trying to achieve with the code snippet below is find a node (in this
case with Key = "ADMIN PAY") and then expand all the parent nodes. The
code below works in that it finds the target node and expands that and
the immediate parent but does not go to the next level.

A further complication is that the node to be found can be at any
level between 2 to 7 and all parent nodes above that should be
expanded.

Any help to overcome this problem will be graetly appreciated


Private Sub CommandButton2_Click()

Dim branch As Variant
Dim ToFind As Variant

ToFind = "ADMIN PAY"

With UserForm1.TreeView1
For Each branch In .Nodes
If branch.Key = ToFind Then
branch.Expanded = True
branch.Parent.Expanded = True
End If
Next
End With

End Sub

Regards

Michael

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 274
Default Treeview - Expand all parents of node

Hi Joel,

Many thanks for helping out on this. I was trying something like the
loop you put in but just not get it to work.

It didn't work exactly as posted but l made a couple of changes and
got it working. I had to declare the variable NewBranch as Object and
rather confusingly had to insert a On Error statement for the loop
which errored out if NewBranch was nothing!

Not sure l fully understand why the loop did not work properly and if
you could throw any light on the subject l would gratefull as l dont
like throwing On error statements into code willy nilly.

Finally l want to select the 'ToFind' node. Any pointers to what the
code segment looks like?

Heres the code l ended up with:

Private Sub CommandButton2_Click()
'Purpose: To find node
Dim branch As Variant
Dim ToFind As Variant
Dim leaf As Variant
Dim NewBranch As Object

ToFind = "ADMIN NON-PAY"

With UserForm1.TreeView1
For Each branch In .Nodes
If branch.Key = ToFind Then
branch.Expanded = True
branch.Parent.Expanded = True
On Error Resume Next
Set NewBranch = branch
Do While Not NewBranch Is Nothing
Set NewBranch = NewBranch.Parent
NewBranch.Expanded = True
Loop
On Error GoTo 0
End If
Next
End With


Regards

Michael




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Treeview - Expand all parents of node

I can't seem to get treeview working on my PC. The On Error is required
because Nothing doesn't seem to work for TreeView. You need to add a watch
item on NewBranch and step though the code and see what parent is equal to
when you get to the top or find anther property that you can use when you get
to the top.

"michael.beckinsale" wrote:

Hi Joel,

Many thanks for helping out on this. I was trying something like the
loop you put in but just not get it to work.

It didn't work exactly as posted but l made a couple of changes and
got it working. I had to declare the variable NewBranch as Object and
rather confusingly had to insert a On Error statement for the loop
which errored out if NewBranch was nothing!

Not sure l fully understand why the loop did not work properly and if
you could throw any light on the subject l would gratefull as l dont
like throwing On error statements into code willy nilly.

Finally l want to select the 'ToFind' node. Any pointers to what the
code segment looks like?

Heres the code l ended up with:

Private Sub CommandButton2_Click()
'Purpose: To find node
Dim branch As Variant
Dim ToFind As Variant
Dim leaf As Variant
Dim NewBranch As Object

ToFind = "ADMIN NON-PAY"

With UserForm1.TreeView1
For Each branch In .Nodes
If branch.Key = ToFind Then
branch.Expanded = True
branch.Parent.Expanded = True
On Error Resume Next
Set NewBranch = branch
Do While Not NewBranch Is Nothing
Set NewBranch = NewBranch.Parent
NewBranch.Expanded = True
Loop
On Error GoTo 0
End If
Next
End With


Regards

Michael





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
moving treeview node Ernst Excel Programming 0 October 28th 08 06:17 PM
treeview node tag hold array? RB Smissaert Excel Programming 5 June 27th 05 12:32 AM
delete a node in treeview mark Excel Programming 1 May 15th 05 04:10 AM
How to add a sub-node of a treeview control JohnDing[_2_] Excel Programming 1 December 20th 04 09:30 AM
TreeView: add more than one node to nodes collection Peter[_25_] Excel Programming 3 September 5th 03 12:15 PM


All times are GMT +1. The time now is 05:37 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"