UserForm Initialize Code for VBA TreeForm Help
Hi,
I am new to VBA and need to create a VBA Treeform that uses parent and child nodes that when selected populates text boxes with information, drawn from excel worksheets.
The issue I'm facing is that the code I've written returns a Run-time 1004 Application Defined or Object Defined Error.
My spreadsheet contains Title headers in columns A-H, row 1 as follows - A-D, E-H, I-L and so on.
In each row underneath the headers are the names of companies correlating to the alphabetized heading - i.e. Disney would fall in the A-D heading.
The Columns would populate the parent nodes, whilst the rows would populate the child nodes; however the child rows are not finite, so hard coding is not an option, as I need the userform/nodes to update automatically if I add further companies to the spreadsheet.
The following code is for the parent nodes - which works fine.
Private Sub UserForm_Initialize()
Worksheets("Sheet1").Activate
'Parent Nodes
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 1).Value, Text:=Sheet1.Cells(1, 1).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 2).Value, Text:=Sheet1.Cells(1, 2).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 3).Value, Text:=Sheet1.Cells(1, 3).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 4).Value, Text:=Sheet1.Cells(1, 4).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 5).Value, Text:=Sheet1.Cells(1, 5).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 6).Value, Text:=Sheet1.Cells(1, 6).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 7).Value, Text:=Sheet1.Cells(1, 7).Value
TreeView1.Nodes.Add Key:=Sheet1.Cells(1, 8).Value, Text:=Sheet1.Cells(1, 8).Value
I need help with the subsequent coding as it is returning the aforementioned error;
'Child Nodes
Call FillChildNodes(1, "A-D")
Call FillChildNodes(2, "E-H")
Call FillChildNodes(3, "I-L")
Call FillChildNodes(4, "M-P")
Call FillChildNodes(5, "Q-T")
Call FillChildNodes(6, "U-X")
Call FillChildNodes(7, "Y-Z")
Call FillChildNodes(8, "Num")
End Sub
Sub FillChildNodes(ByVal col As Integer, ByVal heading As String)
'Look for last row with data title
Dim LastRow As Long
With Sheet1
LastRow = .Cells(.Rows.Count, col).End(x1UP).Row
End With
Dim counter As Integer
counter = 1
'Loop round and add child nodes
For Each Title In Range(Cells(2, col), Cells(LastRow, col))
TreeView1.Nodes.Add Sheet1.Cells(1, col).Value, tvwChild, heading + CStr(counter), Name
counter = counter + 1
Next Title
End Sub
A different code may be needed but I'm not sure as a) I'm self tutoring and b) my experience with VBA is limited.
Any help would be appreciated
Recent comments
5 years 41 weeks ago
6 years 27 weeks ago
6 years 39 weeks ago
6 years 42 weeks ago
6 years 43 weeks ago
6 years 48 weeks ago
7 years 4 weeks ago
7 years 5 weeks ago
7 years 5 weeks ago
7 years 5 weeks ago