u/barcode00

[EXCEL] Looping through rows representing a nested structure
▲ 3 r/vba

[EXCEL] Looping through rows representing a nested structure

In have a table of data in Excel which represents a nested hierarchical structure. The rows are elements in the structure. All elements are five elements deep. The first five columns of the table represent the level/position of the element. For example, column “Level 1” might have a value of "1", "Level 2” a value of “1.1”, and so on, with the fifth column representing the final element (1.1.1.1.1, 1.1.1.1.2, etc). The other columns describe the names, descriptions of the elements.

I am trying to use VBA to loop through these nested elements with the ultimate goal of creating some documentation of this structure within a Word document with additional notes, etc, in a consistent style.

I have created a PivotTable, which may or not be helpful to my outcome, but it does at least let me see the structure of the parent/child elements. Copying this data into Word from the PivotTable does not make it easy to edit or read which is why I am trying to reconstruct it.

My VBA code is below but of course, it outputs the rows from the columns, rather than the parent item they are from. Maybe there is a better approach altogether! Thank you for any guidance

For Each ptItem In pt.PivotFields("Level 1").PivotItems
  Debug.Print ptItem
    For Each ptItem2 In pt.PivotFields("Level 2").PivotItems
      Debug.Print ptItem2.Name
        For Each ptItem2 In pt.PivotFields("Level 3").PivotItems
          Debug.Print ptItem2.Name
        Next
    Next
Next
u/barcode00 — 4 days ago

Starting out

I am just starting out and would welcome any thoughts on my pie. FWRG, CUKX, VFEG (70:20:10). No individual stocks.

u/barcode00 — 20 days ago