Why does my loop not stop?

ellen01

New Member
Joined
Oct 17, 2018
Messages
7
Hello there,

I ran the code before on a spreadsheet with a number of lines. I basically want to remove any lines with the value in column C starting with 'M'. As there wouldn't be two blank rows in a row anywhere on the sheet apart from at the end of the filled range, I used the loop conditions below. So when there are two blank rows in a row, the loop should stop. However, for some reason this loop does not stop but keeps giving me an 'overflow' error after deleting the contents as desired.

P.S. I do not know how to attach files here so cannot provide the test data I'm using to run the code sorry..... Can anyone spot any problems in my code?

Thanks a lot in advance!

Ellen



Code:
Sub DeleteModel()


Dim i As Integer


i = 2


Sheets("sheet1").Select


Do While Cells(i, 3).Value <> "" & Cells(i + 1, 3).Value <> ""
   
    If Cells(i, 3).Value Like "M*" Then
        Call DeleteRow(i)
        If Cells(i, 3).Value = "" Then
        Call DeleteRow(i)
        End If
    Else
        i = i + 1
    End If
    
Loop


End Sub
 
Last edited by a moderator:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try:
Code:
Sub DeleteRows()
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("C1:C" & LastRow).AutoFilter Field:=1, Criteria1:="=M*"
    Range("C2:C" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    Range("C1").AutoFilter
End Sub
 
Upvote 0
Just realised it should be OR not AND
Code:
Do While Cells(i, 3).Value <> "" Or Cells(i + 1, 3).Value <> ""
 
Upvote 0
Glad it's resolved & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top