VBA loop - Quick question

kgkev

Well-known Member
Joined
Jun 24, 2008
Messages
1,291
Office Version
  1. 365
Platform
  1. Windows
I am stepping through and deleting marked rows in my sheet. If I miss the header row the macro runs but fails to delete one highlighted row.

If I include my header, I get a run time error.

I have fixed it my including i = 1 before the loop
...but I cannot get my head around my it fails.

This one missed one of the flagged "DEL" rows"
Code:
 Set rng = Sheets("Stage 2").Range("[COLOR=#ff0000]F2[/COLOR]", Sheets("Stage 2").Range("F5000").End(xlUp))
    
    For counter = 1 To rng.Rows.Count
        If rng.Cells(i) = "DEL" Then
            rng.Cells(i).EntireRow.Delete
        Else
            i = i + 1
        End If
    Next

This one gives a runtime error
Code:
 Set rng = Sheets("Stage 2").Range("[COLOR=#ff0000]F1[/COLOR]", Sheets("Stage 2").Range("F5000").End(xlUp))
    
    For counter = 1 To rng.Rows.Count
        If rng.Cells(i) = "DEL" Then
            rng.Cells(i).EntireRow.Delete
        Else
            i = i + 1
        End If
    Next

This one works
Code:
 Set rng = Sheets("Stage 2").Range("[COLOR=#ff0000]F1[/COLOR]", Sheets("Stage 2").Range("F5000").End(xlUp))
   [COLOR=#ff0000] i = 1[/COLOR]
    For counter = 1 To rng.Rows.Count
        If rng.Cells(i) = "DEL" Then
            rng.Cells(i).EntireRow.Delete
        Else
            i = i + 1
        End If
    Next
 
Last edited:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
When deleting rows it's best to work from bottom to top like
Code:
   Dim i As Long
   With Sheets("Stage 2")
      For i = .Range("F" & Rows.Count).End(xlUp).Row To 2 Step -1
         If .Range("F" & i).Value = "DEL" Then .Rows(i).Delete
      Next i
   End With
Or without looping
Code:
   With Sheets("Stage 2")
      If .AutoFilterMode Then .AutoFilterMode = False
      .Range("F1:F5000").AutoFilter 1, "DEL"
      .AutoFilter.Range.Offset(1).EntireRow.Delete
      .AutoFilterMode = False
   End With
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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