Macro to Delete rows where there are Zeroes from row 2 onwards in Col K

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,589
Office Version
  1. 2021
Platform
  1. Windows
I have written code to delete rows where where are zeroes in Col K from row 2 onwards from sheet "Upload JNL BRT" onwards

When running the macro, I get a compile error "Invalid Next control variable reference"

Next I is highlighted


Code:
 Sub Delete_Zeroes()
  Dim Lr As Long, i As Long, J As Long
  
 
    For i = Sheets("Upload JNL BRT").Index To Worksheets.Count
        With Worksheets(i)

  
  
  
  Lr = Range("A" & .Rows.Count).End(xlUp).Row
  Debug.Print Lr
  For J = Lr To 1 Step -1
    If .Range("K" & J).Value = 0 And Not IsEmpty(.Range("K" & J)) Then _
      .Range("K" & J).EntireRow.Delete
  Next i
End Sub


It would be appreciated if someone could amend my code and advise where I have gone wrong
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You need a Next J before the line Next i

VBA Code:
Next J
Next i
 
Upvote 0
Thanks for your input Kevin

I now get a compile error "End With without With

Please check & advise

Code:
 Sub Delete_Zeroes()
  Dim Lr As Long, i As Long, J As Long
  
 
    For i = Sheets("Upload JNL BRT").Index To Worksheets.Count
With Worksheets(i)
    
  
  Lr = Range("A" & .Rows.Count).End(xlUp).Row
  Debug.Print Lr
  For J = Lr To 1 Step -1
    If .Range("K" & J).Value = 0 And Not IsEmpty(.Range("K" & J)) Then _
      .Range("K" & J).EntireRow.Delete
   End With
      Next J
  Next i
      End With
End Sub
 
Upvote 0
Remove the End With before the Next J and after the Next i and put it between the 2 Nexts

VBA Code:
Next J
End With
Next i
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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