Moving data from Treeview to Listbox with 2 columns

haplc

Board Regular
Joined
May 27, 2004
Messages
71
Dear All,

I have to move data from Treeview to Listbox with 2 columns. Idea is to send node value of treeview to column 1 of list box and tag value from treeview node to column 2 of list box.
I use two codes: THis code transfer node value to Column 1 and tag value to column two. However, code is transfering all the nodes and I am not able to transfer only selected node:

Private Sub CommandButton9_Click()
ListBox3.ColumnCount = 2 'setting two columns

For i = 1 To TreeView2.Nodes.Count - 1

ListBox3.AddItem
ListBox3.List(i - 1, 0) = TreeView2.Nodes(i)
ListBox3.List(i - 1, 1) = TreeView2.Nodes(i).Tag

Next i

End Sub

WIth the following code, I am able to transfer selected node to column 1 of list box. However, when I try to add tag, then it is giving error.

Private Sub CommandButton8_Click()
ListBox3.ColumnCount = 2 'setting two columns

For i = 1 To TreeView2.Nodes.Count - 1
If TreeView2.Nodes(i).Selected Then
ListBox3.AddItem TreeView2.Nodes(i)

End If
Next i

End Sub

Thanks in advance for help

Best regards
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Try this:

VBA Code:
Private Sub CommandButton8_Click()
    ListBox3.ColumnCount = 2 'setting two columns
    
    For i = 1 To TreeView2.Nodes.Count - 1
        If TreeView2.Nodes(i).Selected Then
            ListBox3.AddItem TreeView2.Nodes(i)
            ListBox3.List(ListBox3.ListCount - 1, 1) = TreeView2.Nodes(i).Tag
        End If
    Next i
End Sub

By the way, your code for both buttons skips the last node with this code:
VBA Code:
For i = 1 To TreeView2.Nodes.Count - 1

I don't know if that was on purpose because the last node is not important or for some other reason. However, if you want to look at all nodes, remove the "- 1"

Alternatively, you could try this code that will run through each node (either way is fine):
VBA Code:
Private Sub CommandButton8_Click()
    Dim n As Node
    ListBox3.ColumnCount = 2 'setting two columns
    
    For Each n In TreeView2.Nodes
        If n.Selected Then
            ListBox3.AddItem n
            ListBox3.List(ListBox3.ListCount - 1, 1) = n.Tag
        End If
    Next
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Private Sub CommandButton8_Click()
    ListBox3.ColumnCount = 2 'setting two columns
   
    For i = 1 To TreeView2.Nodes.Count - 1
        If TreeView2.Nodes(i).Selected Then
            ListBox3.AddItem TreeView2.Nodes(i)
            ListBox3.List(ListBox3.ListCount - 1, 1) = TreeView2.Nodes(i).Tag
        End If
    Next i
End Sub

By the way, your code for both buttons skips the last node with this code:
VBA Code:
For i = 1 To TreeView2.Nodes.Count - 1

I don't know if that was on purpose because the last node is not important or for some other reason. However, if you want to look at all nodes, remove the "- 1"

Alternatively, you could try this code that will run through each node (either way is fine):
VBA Code:
Private Sub CommandButton8_Click()
    Dim n As Node
    ListBox3.ColumnCount = 2 'setting two columns
   
    For Each n In TreeView2.Nodes
        If n.Selected Then
            ListBox3.AddItem n
            ListBox3.List(ListBox3.ListCount - 1, 1) = n.Tag
        End If
    Next
End Sub
Many thanks...Both codes are working ..thanks for pointing out the "-1" mistake. I have corrected it

Many thanks again
 
Upvote 0

Forum statistics

Threads
1,214,411
Messages
6,119,346
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top