79090543

Date: 2024-10-15 15:05:55
Score: 2.5
Natty:
Report link

A big thank you to @Ken White. Indeed to backdown rows, one has to monkey with the loop. Please see the following paragraph. Also a big thank you to @rotabor for his variants. His theorems pass the test but fail in practice. They don't take into account variadic rows (having different number of columns) and mixed. However, I would kindly ask that he doesn't delete them, because they are valuable learning materials. They have been immensely helpful. I will also state the same for the comments (all of them).

Because we are iterating backwards and inserting rows above the current row, It is not just the countdown for the rows that matters. As it turns out, the loop (which ever you choose to use) will have you know, that you will not go past your set limit of 0 even if you needed to! And just so you know it doesn't make mistakes on which row it visits, if you are required to overstep your limit to get to the newly inserted row (-1 or 0) and you are at the top of the chain (0 or 1), it will terminate at the limit and won't care.

And so, if you are counting backwards, then the limit shall also back up a notch every time you add a new row from the top of the last one (when logically you are at row 1 but internal counter is at 0 or some other like -1 because you update your range)! See, its the little things we miss. Who knew you'd have to backup your limit?

So here is the solution.

Sub ReverseIterateAndInsertRowRestart()

' Firstly, sort the fields if rows have mixed data to avoid duplicates.
' Use a procedure that looks good to you here: https://stackoverflow.com/questions/79082783.
  Call SortBlanksOnTop
  
' Set up your variables and turn off screen updating.
  Dim Cell As Range
  Dim addedRow As Boolean
  Application.ScreenUpdating = False
  
' Initialize.
  a = 0
  b = r.Rows.Count
  c = r.Columns.Count
  
' Iterate through each row from bottom to top.
  Do While b > a
    ' Assume no row has been added
      addedRow = False
          
    ' Iterate through each column from right to left within that row
      For d = c To 1 Step -1
          Set Cell = r.Cells(b, d)
        ' Use non-formular cells that have a column to the left...
          If SomeConditionIsMet Then
                ' Row #
                  e = Cell.Row
                
                ' Copy the current row
                  w.Rows(e).Copy
                  
                ' Insert the copied row above the current row
                  w.Rows(e).Insert Shift:=xlDown
                
                ' Clear the cell in the inserted row at the current cell's column
                  w.Cells(e, Cell.Column).ClearContents
                
                ' Update the range to include the new row
                  Set r = w.Range(r.Cells(1, 1), r.Cells(r.Rows.Count + 1, r.Columns.Count))
                
                ' Indicate that a row has been added
                  addedRow = True

                ' Restart loop for current row
                  Exit For
        End If
      Next d
    ' Count down
      b = b - 1
    ' If a row was added, adjust the row limit to account for the new row
      If addedRow And b < 1 Then a = b - 1
  Loop
  
' Turn screen updating back on.
  Application.ScreenUpdating = True

End Sub

Reasons:
  • Blacklisted phrase (0.5): thank you
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • User mentioned (1): @Ken
  • User mentioned (0): @rotabor
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: John Miller