How to grab the value of the top unindented row value (Parent) after checking that the preceding cells are indented?

Viewed 25

I currently have 2 columns:

  1. Parent Measurement - value should be the top/parent value in the measurement column that isn't indented
  2. Measurement - Nonindented values are parents, indented values belong under unindented value.

Currently I'm able to read if a certain cell under the measurement column is indented and then I put a 1 to flag it. However, instead of assigning a 1, how can assign the value of the parent measurement that isn't indented?

The picture is how I would like the end result to look like, the second one is how I currently my code does it. Thank you!

Set rngCol = wssc.Range("DRT_Table[Measurement]")
  For Each cl In rngCol
    If cl.IndentLevel = 2 Then
    cl.Offset(0, -1).Value = "1" '<--This is the part I need help with'
    End If
  Next cl

enter image description here

enter image description here

1 Answers

Try something like this - if cl doesn't have an IndentLevel of 2, then assign its value to a variable parentMeasure, otherwise assign parentMeasure to cl.Offset(0, -1).

For Each cl In rngCol
   Dim parentMeasure As String
   If cl.IndentLevel = 2 Then
      cl.Offset(0, -1).Value = parentMeasure
   Else
      parentMeasure = cl.Value
   End If
Next
Related