A report produces a sheet that looks like this:
Format is inherited from source data, data is in cols A, D and G, with A merging ABC, D merging DEF and G on its own.
I want to keep only the lines highlighted in blue. I want the macro to check the contents of G:G and if Cell.Text = "" then entirerow.delete, else leave it alone.
Only problem is when it deletes row 2, then row 3 becomes row 2, meaning that it doesn't delete it, because it's already checked row 2. What was row 4 now becomes row 3, which is the next one to check, and it sees that text is there and doesn't delete, moves on to row four, deletes it, row 5 becomes row 4, remains unchecked, it moves on to row 6 (now row 5), sees that's blank, deletes it, but row 7 now becomes row 6 and isn't deleted... and so on.
The only way I've found around it is to use GOTO to restart the loop, then to use another GOTO to get out of the loop early. I appreciate that probably isn't the most elegant solution.
iRange = Application.Workbooks(2).Worksheets(2).Cells(Rows.Count, "A").End(xlUp).Row + 1
Debug.Print (iRange)
RestartLoop:
For Each rCell In Application.Workbooks(2).Worksheets(2).Range("G2:G" & iRange)
If Not rCell.Offset(0, -3).Value = "" Then
If rCell.Text = "" Then
Debug.Print (rCell.Address)
rCell.EntireRow.Delete
GoTo RestartLoop
End If
Else0
GoTo LeaveLoop
End If
Next rCell
LeaveLoop:
'Do the next thing
(code is Option Explicit: Dim iRange As Integer Dim rCell as Range in declarations at top of code)
Here's the debug.print:
26
$G$2
$G$2
$G$3
$G$3
$G$3
$G$3
$G$3
$G$3
$G$3
$G$3
$G$3
$G$4
$G$4
$G$4
$G$4
$G$4
$G$6
$G$6
$G$7
If anyone knows of a solution, and can also explain it for a layman, I'd appreciate it. The solution needs to be dynamic, because the original sheet produced is a different length each time, with a different number of blank and blue cells each time.
I have searched around a little but either I'm not understanding the answers fully, or they seem to give me the same problem.