Compile error: For without Next

NewbieVBie

Board Regular
Joined
Jan 30, 2003
Messages
65
i am consistantly getting a Compile error:
saying:

For Without Next.
what does this mean and how can i resolve it?
(the debug highlights the End Sub line of the code)
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
It's probably because you have a For loop and haven't correctly structured it with a Next e.g. example of incorrect For Next :

<pre>Sub Test()

For x = 1 To 5

MsgBox "Value of x is " & x


End Sub</pre>

This should be:

<pre>Sub Test()

For x = 1 To 5

MsgBox "Value of x is " & x

Next x

End Sub</pre>

I hope this makes sense. If not, it will help if you post your code.
 
Upvote 0
The code you mentioned compiles fine and doesn't have a For Next loop in it.
 
Upvote 0
No. Can you post the code you are using, and the exact error message you get and where. The link to the thread you posted contains several different pieces of code so post the one you're using right now.

_________________
<font face="Arial Black" color="#0000FF">Hope this helps,
DK</font>

This message was edited by dk on 2003-02-21 06:00
 
Upvote 0
i am using this code:
Private Sub DAYREQUESTTB_Change()


Dim i As Integer
Dim DaysRequest As Integer
Dim DaysRemaining As Integer
Dim ThisCell As Range
For Each ThisCell In Range("C1:C100,G1:G100,K1:K100,O1:O100,S1:S100,W1:W100,AA1:AA100,AE1:AE100,AI1:AI100,AM1:AM100,AQ1:AQ100,AU1:AU100,AY1:AY100,BC1:BC100")
If ThisCell.Value = DAYREQUESTTB.Value Then
i = ThisCell.Row
Exit For
End If
DaysRemaining = Cells(i, 6).Value
DaysRequest = InputBox("Welcome " & DAYREQUESTTB.Value & ". You are entitled to " & Cells(i, 4).Value & " days off. You have used " _
& Cells(i, 5).Value & " days off and have " & DaysRemaining & ". Please enter the number of days you are requesting", "Welcome " & DAYREQUESTTB.Value)
If DaysRequest > DaysRemaining Then
MsgBox "You only have " & DaysRemaining & " days left. Your request has exceeded this amount.", vbOKOnly + vbCritical, "Error"
Exit Sub
End If
Cells(i, 5).Value = Cells(i, 5).Value - DaysRequest

End Sub

and i get this message:

Compile Error:
For without Next
 
Upvote 0
i am using this code:
Private Sub DAYREQUESTTB_Change()


Dim i As Integer
Dim DaysRequest As Integer
Dim DaysRemaining As Integer
Dim ThisCell As Range
For Each ThisCell In Range("C1:C100,G1:G100,K1:K100,O1:O100,S1:S100,W1:W100,AA1:AA100,AE1:AE100,AI1:AI100,AM1:AM100,AQ1:AQ100,AU1:AU100,AY1:AY100,BC1:BC100")
If ThisCell.Value = DAYREQUESTTB.Value Then
i = ThisCell.Row
Exit For
End If
DaysRemaining = Cells(i, 6).Value
DaysRequest = InputBox("Welcome " & DAYREQUESTTB.Value & ". You are entitled to " & Cells(i, 4).Value & " days off. You have used " _
& Cells(i, 5).Value & " days off and have " & DaysRemaining & ". Please enter the number of days you are requesting", "Welcome " & DAYREQUESTTB.Value)
If DaysRequest > DaysRemaining Then
MsgBox "You only have " & DaysRemaining & " days left. Your request has exceeded this amount.", vbOKOnly + vbCritical, "Error"
Exit Sub
End If
Cells(i, 5).Value = Cells(i, 5).Value - DaysRequest

End Sub

and i get this message:

Compile Error:
For without Next

the debug highlights the last end sub line in blue
This message was edited by NewbieVBie on 2003-02-21 06:04
 
Upvote 0
thanks that solved that problem but now i get a error 1004
application defined or object defined error

on this line:
DaysRemaining = Cells(i, 6).Value

i believe it is because the column indicator is wrong, it is supposed to be looking up data in every fourth column from column F
so which value would change and to what?
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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