Reputation: 1178
I am not sure why there is an invalid key error when I do convert it to a string just like how the examples do it.
Private Sub UserForm_Initialize()
Dim i As Long, j As Long, Total_rows_Column As Long
Dim unique_keys As Long
For i = 1 To 5
TreeView1.Nodes.Add Key:=Worksheets("Sheet1").Cells(1, i), Text:=Worksheets("Sheet1").Cells(1, i)
Next i
unique_keys = 0
For i = 1 To 5
Total_rows_Column = Worksheets("Sheet1").Range(Col_Letter(i) & Rows.Count).End(xlUp).Row
For j = 2 To Total_rows_Column
unique_keys = unique_keys + 1
'ERROR IN THE LINE BELOW************
TreeView1.Nodes.Add Worksheets("Sheet1").Cells(1, i).Value, tvwChild, CStr(unique_keys), Worksheets("Sheet1").Cells(j, i).Value
Next j
Next i
End Sub
Upvotes: 1
Views: 1471
Reputation: 29286
It seems that the key
of the treeNode may not contain only digits (trial and error, havn't found any reference). Try something like
TreeView1.Nodes.Add Worksheets("Sheet1").Cells(1, i).Value, _
tvwChild, "Key" & CStr(unique_keys), _
Worksheets("Sheet1").Cells(j, i).Value
Update:
The key of a node is used as the name of the node, and a name may not be a number. You can write TreeView1.Nodes("Key1").Text = "MyTest"
to access the node, but if the name would be 1
, the compiler coudn't tell if you mean the node with name 1 or with index 1
Upvotes: 2
Reputation: 43585
This is some minimal code, that works, producing this:
Private Sub UserForm_Initialize()
Dim i&, j&
For i = 1 To 5
Me.TreeView1.Nodes.Add Key:="key" & i, Text:="text" & i
Next i
For i = 1 To 5
For j = 2 To 3
TreeView1.Nodes.Add "key" & i, tvwChild, "Some More Text" & j & i, "Even More" & j
Next j
Next i
End Sub
It seems that the key should contain non-numeric characters and no empty values - VBA treeview loading issue - invalid key.
Upvotes: 2