Re: Populating a treeview control based on a data range on two columns
Wasn't interested in the data... Just the treeview. Everything else could have been deleted, but no matter; glad it finally works for you
Re: Populating a treeview control based on a data range on two columns
Wasn't interested in the data... Just the treeview. Everything else could have been deleted, but no matter; glad it finally works for you
Re: Populating a treeview control based on a data range on two columns
This thread has helped immensely with a treeview form that I'm working on, but I'm running into a problem that I was hoping you could help me with. I have four or five columns that I need to load into a treeview. The first three columns seem to load fine, but when I get to the last two, the treeview doesn't populate all the children nodes. I've attached what I've done so far. Basically, each project has task numbers and may have parent task numbers. I tried to add some logic so that when there is no parent task number in column E, then set nChild to the task number in column F. Otherwise, if there is a parent task number, then set nChild to the parent task number in column E. Here is your code that I adjusted:
Private Sub RefreshHierarchy_Click()
Dim cell As Range
Dim nParent As Node
Dim nChild As Node
Application.ScreenUpdating = False
'// Simple code - An error will be raised if attempt to add a node with a key that already exists
On Error Resume Next
'// Loop through Range A1, A(Number of used rows) - Edit to suit your requirements
For Each cell In Sheets("Planning - Projects&Tasks").Range("A5:A6392")
'// Try and add a node. Key is the cell value.
'// Keys must be unique, but also MUST be text. Cannot be numeric. If a cell
'// contains numbers, then use '_' or any other text before the cell value to make it a string
'***TOP HIERARCHY LAYER***
Set nParent = tv.Nodes.Add(, , cell.Value, cell.Value)
'/ If an error occurred then this key is already in use
If Err.Number <> 0 Then
'// So set a reference to that node - need to add the child node
Err.Clear
Set nParent = tv.Nodes(cell.Value)
End If
'// If you want Subcategories unique then use this
'***INVESTMENT PRIORITY LAYER***
Set nChild = tv.Nodes.Add(nParent, tvwChild, cell.Offset(0, 1).Value, cell.Offset(0, 1).Value)
'// otherwise
'// Set nChild = tv.Nodes.Add(nParent, tvwChild, , cell.Offset(0, 1).Value)
'// Clear any error if adding unique child nodes - otherwise it could interfere with adding parent nodes.
Err.Clear
Next
For Each cell In Sheets("Planning - Projects&Tasks").Range("B5:B6392")
Set nParent = tv.Nodes.Add(, , cell.Value, cell.Value)
If Err.Number <> 0 Then
Err.Clear
Set nParent = tv.Nodes(cell.Value)
End If
Set nChild = tv.Nodes.Add(nParent, tvwChild, cell.Offset(0, 1).Value, cell.Offset(0, 1).Value)
Err.Clear
Next
For Each cell In Sheets("Planning - Projects&Tasks").Range("C5:C6392")
Set nParent = tv.Nodes.Add(, , cell.Value, cell.Value)
If Err.Number <> 0 Then
Err.Clear
Set nParent = tv.Nodes(cell.Value)
End If
Set nChild = tv.Nodes.Add(nParent, tvwChild, cell.Offset(0, 1).Value, cell.Offset(0, 1).Value)
Err.Clear
Next
For Each cell In Sheets("Planning - Projects&Tasks").Range("D5:D6392")
Set nParent = tv.Nodes.Add(, , cell.Value, cell.Value)
If Err.Number <> 0 Then
Err.Clear
Set nParent = tv.Nodes(cell.Value)
End If
If cell.Offset(0, 1).Value = "_(blank)" Then
Set nChild = tv.Nodes.Add(nParent, tvwChild, cell.Offset(0, 2).Value, cell.Offset(0, 2).Value)
ElseIf cell.Offset(0, 1).Value <> "_(blank)" Then
Set nChild = tv.Nodes.Add(nParent, tvwChild, cell.Offset(0, 1).Value, cell.Offset(0, 1).Value)
End If
Err.Clear
Next
Application.ScreenUpdating = True
End Sub
I feel like I'm so close to getting it right and I've spent a couple hours trying to get it to work without success. Any help you can offer would be greatly appreciated! Thank you so much!!!
Re: Populating a treeview control based on a data range on two columns
Welcome to OzGrid, christopheralan.
The policy here is you do not post questions in threads started by other members - the reason being we like titles to accurately describe the contents of a thread and additional questions tend to drag the topic away from that.
Please start your own thread. Give it an accurate and concise title that describes your issue and explain your problem fully. If you think this, or any other thread, can help clarify your probem, you can include a link by copying the URL from the address bar of your browser and pasting into your message.
When you post code in a message you are asked to use Code Tags. These format and indent the code making it easy to read. Code tags are added when editing a meesage simply by highlighting the code and clicking the '#' button on the toolbar above the edit box.
You refer to data in your workbook in your message ("no parent task number in column E, then set nChild to the task number in column F..."), it would help if you uploaded a copy with any identifiable data anonymised and containing a representative sample of the data.
[sw]*[/sw]
Re: Populating a treeview control based on a data range on two columns
Sorry about that! I'll start a new thread. Thanks!
Don’t have an account yet? Register yourself now and be a part of our community!