VBA Compile Error: Next without For?

rmfrase

Board Regular
Joined
Jul 6, 2006
Messages
132
Office Version
  1. 365
Platform
  1. Windows
I'm puzzled on this as I do not understand where the error is comming from.
I have a macro that is pulling information between worksheets, but the For Next statements appear clean. However, when I run, I get the [Compile Error: Next without For] message

Condensed Example

'Let's say we have (4) worksheets within the workbook
ShtNum = Sheets.Count

' We'll start pulling data off Sheet2 to start
For Loop1 = 2 to ShtNum
Sheets(Loop1).Select
Sheets(Loop1).Activate

' Let's get some data
Set d = Cells.find(What:="Created", LookAt:=xlWhole)
CrtDate = Cells(2, d.Column)

' Switch back to the 1st sheet and place the data in specific areas.
Sheets(1).Select
Sheets(1).Activate
Cells(3, 2) = CrtDate
Selection.NumberFormat = "mm/dd/yyyy"

'go Color the Cells
GoSub colorbrtyel

' Get additional Data on other sheets.

' Return back to Sheet1 and place the data.

'do a small loop within a Loop
for I= 2 to last row
'format cells
next I



' do some gosubs to format the data.

Sheets(Loop1).Select
Sheets(Loop1).Activate


Next Loop1


'-------------------------------------------------
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hello,

The Loop near the top, Loop1 is missing Next Loop1.

Where ever you need the loop to happen in you code, Place Next Loop1.

-Jeff
 
Upvote 0
Hello,

The Loop near the top, Loop1 is missing Next Loop1.

Where ever you need the loop to happen in you code, Place Next Loop1.

-Jeff


Jeff, the Next Loop1 is at the bottom.

ie:

For Loop1 = 2 to ShtNum
gosub 1
gosub 2
for I= 2 to lastrow
next I
Next Loop1

'continue program



'1
return
exit sub

'2
return
exit sub

end Sub
 
Upvote 0
Why do you have Gosub in the code?

I can't remember the last time I saw that used in any code and don't think I've ever seen it in VBA.

It might even be what's causing the problem, especially if it causes the code to leap out of the loop.
 
Upvote 0
Here's some code that compiles so you can step through it:
Code:
Sub x()
    Dim ShtNum      As Long
    Dim Loop1       As Long
    Dim lastrow     As Long
    Dim i           As Long
 
    ShtNum = 3
    lastrow = 4
 
    For Loop1 = 2 To ShtNum
        GoSub 1
        GoSub 2
        For i = 2 To lastrow
        Next i
    Next Loop1
 
    ' other code
    
    Exit Sub
 
1
    Return
 
2
    Return
End Sub
 
Upvote 0
Issue Resolved.

Discovered a misplaced "With Selection.Interior" statement embedded within all of the code. but I'm not sure how it triggered the "Next without For" error.


Thanks for all of your help.
 
Upvote 0
If it didn't have a End With it would trigger an error.

If there were no loops in the code you would get a message specific to With/End With.
 
Upvote 0

Forum statistics

Threads
1,224,614
Messages
6,179,906
Members
452,949
Latest member
beartooth91

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