My goal is to delete all rows between text QA and text QS, including row with text QS. Text is in column B and the number of rows between them is dynamic. All rows without either text should not be deleted.
This is the code I am using (from MrExcel), but I am getting the following error: "Compile error: Variable not defined" and the r in the r=1 code is highlighted.
This is the code I am using (from MrExcel), but I am getting the following error: "Compile error: Variable not defined" and the r in the r=1 code is highlighted.
VBA Code:
'Delete rows below QA until you get to QS
Sub DeletebyBookends_KEEP_LAST()
Dim strStart As String, strEnd As String
Dim DELETEMODE As Boolean
Dim DelRng As Range
strStart = "QA"
strEnd = "QS"
DELETEMODE = False
For r = 1 To Range("B" & Rows.Count).End(xlUp).Row 'first to last used row
If Range("B" & r).Value = strStart Then DELETEMODE = True
If Range("B" & r).Value = strEnd Then DELETEMODE = False
If DELETEMODE Then
'Create a Delete Range that will be used at the end
If DelRng Is Nothing Then
Set DelRng = Range("A" & r)
Else
Set DelRng = Application.Union(DelRng, Range("A" & r))
End If
End If
Next r
'Delete the Range compiled from above
If Not DelRng Is Nothing Then DelRng.EntireRow.Delete xlShiftUp
End Sub