1

I want to delete specified number of rows after a found value in a column. I find a search value in A3 then I want to delete rows from 4th row to specified number of rows say from 4 th row to 11th row. My code deletes alternate rows. If I specify ~Rows("cnt1:cnt2") it gives error. Need guidance to correct the error in my code. Images before and after code run are given below. It should delete consecutive rows containing aa,bb,cc,dd.....

image before code run image after code run

Code followed by me is given below.

 Sub Asearch()
Dim found As Range
Dim FR As Long
Dim cnt1 As Long
Dim cnt2 As Long
   Set found = Sheets("Sheet1").Columns("A").Find(what:="Swati", LookIn:=xlValues, lookat:=xlWhole)
    If found Is Nothing Then
       MsgBox "Not found"
       Else
       MsgBox "Found on row " & found.Row
       FR = found.Row
       cnt1 = FR + 1
       Debug.Print cnt1
       cnt2 = 11
       Do While cnt1 <= cnt2
          Rows(cnt1).EntireRow.Delete
          cnt1 = cnt1 + 1
          Debug.Print cnt1
       Loop
    End If
End Sub  

EDIT of the post based on @micstr guidance. Problem is solved. Updated code is given below.

Sub Asearch2()
Dim found As Range
Dim FR As Long
Dim cnt1 As Long
Dim cnt2 As Long
   Set found = Sheets("Sheet1").Columns("A").Find(what:="Swati", LookIn:=xlValues, lookat:=xlWhole)
    If found Is Nothing Then
       MsgBox "Not found"
       Else
       MsgBox "Found on row " & found.Row
       FR = found.Row
       cnt1 = FR + 1
       Debug.Print cnt1
       cnt2 = 11
       Do While cnt2 >= cnt1
          Rows(cnt2).EntireRow.Delete
          cnt2 = cnt2 - 1
          Debug.Print cnt2
       Loop
    End If
End Sub

1 Answer 1

1

You have an error in your loop as you keep pushing the counter forward. As you delete entire row the index moves up. So

1 aa
2 bb
3 cc

As you delete aa, bb moves up to place 1 so now adding one to counter cntl is looking now at position 2. And you delete cc leaving bb. Which has moved up to one place.

1 bb
2 cc

To fix this perhaps you want to rather mark All Rows at once and delete them as one block. Or stay with Delete Entire Row and fix how you are moving your counters. (Thinking about it you will see cnt2 is changing i.e. moving up cnt2 = cnt2 - 1 as you delete rows).

Or another approach which may be easier to debug is to start from the bottom and work upwards not downwards.

Let me know if you need further help.

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

3 Comments

I tried number of rows delete by trying code ~Rows("cnt1:cnt2").EntireRow.Delete~ but it gives error , seems not to be correct syntax. Please suggest the code modification for this purpose . My level in VBA is basic only. please help.
Thanks for your valuable guidance. Problem is solved and I have uploaded the edited code.
Great - glad you cracked it. Debugging can be hell sometimes!

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.