2

I am trying to find the cells value "Details" and delete that row along with the 3 rows below it.

The code I current have works but once there are no longer any cells with "Details", I get the error box asking me to end the macro or debug.

How do I fix current code and eliminate the error message box from popping up.

My current code is below.

Dim StartRange As String
Dim EndRange As String
For Each Cell In ActiveSheet.Range("A1:A1000")
    Cells.Find(What:="Details").Select
    StartRange = ActiveCell.Address
    Selection.Offset(3, 75).Select
    EndRange = ActiveCell.Address
    ActiveSheet.Range(StartRange & ":" & EndRange).Select
    Selection.EntireRow.Delete
Next Cell
3
  • What is the error message exactly, and which line/instruction is the VBA editor highlighting when you click "debug"? Commented Sep 4, 2018 at 19:46
  • 3
    That said, this would be a relevant read. Commented Sep 4, 2018 at 19:46
  • It's not clear what your code is doing. It's iterating over 1000 cells but searches for Details all over the sheet. Be aware, that Find searches for partial result. Commented Sep 4, 2018 at 19:58

1 Answer 1

2

Try this - note, it's best to fully qualify Range with your Workbook and Worksheet name (i.e. Workbooks("MyWorkbook").Worksheets("MyWorksheet").Range().

Option Compare Text
Sub Test()

Dim i As Long

For i = 10000 To 1 Step -1
    If Range("B" & i).Value = "details" Then
        Range("B" & i & ":B" & i + 3).EntireRow.Delete
    ElseIf Range("V" & i).Value = "pending renewal" Then
        Range("V" & i & ":V" & i + 7).EntireRow.Delete
    End If
Next i

End Sub
Sign up to request clarification or add additional context in comments.

16 Comments

Range(Cells(i, "A"), Cells(i - 3, "A")) is lookin' better :)
@JohnyL Either of them would look better with fully qualified calls to Range or Cells.
@Comintern OP never provided any workbook or worksheet names, so an unqualified Range might as well be just as effective as his use of ActiveSheet - I think it's beyond the point until he provides more details.
I guess i + 3 - not i - 3
@JohnyL Stepping backwards, but I did change it to Range("A" & i - 3 & ":A" & i).EntireRow.Delete ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.