title: Populate treeview control with hierarchical data from Excel Sheet
body: <p>I am currently using a treeview control from <a href="http://www.jkp-ads.com/Articles/treeview.asp" rel="nofollow noreferrer" title="JKP">Treeview Control from JKP</a>. Trying to populate the tree with hierarchical data from an Excel Sheet, I encountered this problem: the tree I populated does not reflect the data. In this picture <code>AP0004</code> should be under <code>TP0002</code></p>
  
  <p><a href="https://i.stack.imgur.com/mcknM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mcknM.png" alt="Populate a treeview control in VBA"></a></p>
  
  <p>The code I've tried is this</p>
  
  <pre><code>For Each c In Sheet14.Range("A2:A" &amp; Sheet14.Range("A" &amp; Rows.Count).End(xlUp).Row)
  
      On Error Resume Next
      'Populate level 1
      Set cRoot = .AddRoot(sKey:=c.Value2, vCaption:=c.Value2)
  
      Set cExtraNode = cRoot.AddChild("ML_" &amp; c.Value2, "Meilensteine")
  
      'Populate level 2
      Set cNode = cRoot.AddChild(c.Offset(, 1).Value2, c.Offset(, 1).Value2)
  
      'Populate level 3
      If cNode.Level = 2 Then Set cNode = cNode.ParentNode
  
      If Not IsEmpty(c.Offset(, 2).Value2) Then
          Set cNode = cNode.AddChild(c.Offset(, 2).Value2, c.Offset(, 2).Value2)
      End If
  
      'Populate level 4
      If Not IsEmpty(c.Offset(, 3).Value2) Then
          If cNode.Level = 2 Then
              Set cNode = cNode.AddChild(c.Offset(, 3).Value2, c.Offset(, 3).Value2)
              Set cNode = cNode.ParentNode
          ElseIf cNode.Level = 1 Then
              Set cNode = cNode.Child.AddChild(c.Offset(, 2).Value2, c.Offset(, 2).Value2)
              Set cNode = cNode.Child.AddChild(c.Offset(, 3).Value2, c.Offset(, 3).Value2)
              Set cNode = cNode.ParentNode
          End If
      End If
  Next
  </code></pre>
  
  <p>There must be something wrong with my approach here. Any suggestion?</p>