Compile error "Next without For"

jimayers

Board Regular
Joined
Nov 14, 2010
Messages
99
Hi there,
When I run this code I get the compile error "next without for" and I can't figure out why
Code:
Sub Find_Class_A1_then_Hide()
Application.ScreenUpdating = False
Dim r, Lastrow, cLastrow, tRow As Long
Lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    
For r = 1 To Lastrow
cLastrow = Cells(r + 6, "E").End(xlDown).Row
tRow = r - 4
MsgBox ("r= " & r & " - " & "cLastrow = " & cLastrow)
    If Cells(r, "C") = "Basic I" Then
        Rows(2).Hidden = True
        Rows(tRow & ":" & cLastrow).Hidden = True
Next r
Application.ScreenUpdating = True
End Sub
Thanks for your help - I just can't see what I'm overlooking.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Missing End If statement before Next r statement

Code:
For r = 1 To Lastrow
cLastrow = Cells(r + 6, "E").End(xlDown).Row
tRow = r - 4
MsgBox ("r= " & r & " - " & "cLastrow = " & cLastrow)
    If Cells(r, "C") = "Basic I" Then
        Rows(2).Hidden = True
        Rows(tRow & ":" & cLastrow).Hidden = True
[COLOR=#0000ff]    End If[/COLOR]
Next r
 
Last edited:
Upvote 0
Missing End If statement before Next r statement

Code:
For r = 1 To Lastrow
cLastrow = Cells(r + 6, "E").End(xlDown).Row
tRow = r - 4
MsgBox ("r= " & r & " - " & "cLastrow = " & cLastrow)
    If Cells(r, "C") = "Basic I" Then
        Rows(2).Hidden = True
        Rows(tRow & ":" & cLastrow).Hidden = True
[COLOR=#0000ff]    End If[/COLOR]
Next r
That was it - thanks!!!
 
Upvote 0
... because

It is compiling within the IF block of code.
When it encounters the NEXT, it looks back to find the equivalent FOR (within that block of code).
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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