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

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.
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,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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