For Each .... Next - spasmodic results

SharonJ

New Member
Joined
May 12, 2011
Messages
4
This is a very simple procedure, yet for some rows of the spreadsheet the code works, and for others it doesn't. If I stop the procedure at that point and go back to the row that didn't work initially and re-run it, it normally works on that row, but then doesn't work on a row further down. Can I do something to make this work 100%? Thanks Sharon

Dim r as Range

Worksheets("Plan 1").Range("Q7").Activate

For Each r in Range ("Q7:Q350")
If r.text = "empty" then
Selection.EntireRow.Delete
Else
Selection.Offset(1,0).Select
End If
Next
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Sharon

When deleting you need to loop backwards, you can't do that with a For...Next loop.

Try this.

Code:
Dim I As Long
 
For I = 350 To 7 Step -1
 
      Set r = Range("Q & I)
 
      If r.Value = "empty" Then
          r.EntireRow.Delete
      End if
 
Next I
 
Upvote 0
Thanks Norrie.

But now I'm getting the Runtime Error 1004 Method Range of Object '_Global' failed.
 
Upvote 0
Code:
Sub test()
Dim I As Long
 
For I = 350 To 7 Step -1
 
      Set r = Range("Q" & I)
 
      If r.Value = "empty" Then
          r.EntireRow.Delete
      End If
 
Next I

End Sub

Maybe... Norrie's stuff is way to complex for me, but that might be it. (Just added a " after the Q)
 
Upvote 0
Sharon

Welcome to the MrExcel board!

Another possible way without looping at all ..
Rich (BB code):
Sub delete_rows()
    Application.ScreenUpdating = False
    With Worksheets("Plan 1").Range("Q6:Q350")
        .AutoFilter Field:=1, Criteria1:="=Empty"
        .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
        .AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Nope, it's my fault - I missed the closing quote for Q.:oops:
 
Upvote 0
Holy cow.... I got one right!

*dusts off cap*
*Insert feather*
*Struts around office*

Nice to know that eventually some of this stuff is starting to sink in a bit.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,750
Members
452,940
Latest member
rootytrip

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