Next Without For Error

W

wfgfreedom

I am getting an error message of "Next without For" on the Next
RowCount Statement at the end of the following code. Can anyone tell
me why?

Frank

Sub Macro()

LastRw = 54
NewRowCount = 38
For RowCount = NewRowCount To LastRw Step 2
Ahead = Cells(RowCount, 5) - Cells(RowCount, 4)
If Ahead >= 0 Then
Cells(RowCount, 6) = 0
ElseIf Ahead < 0 Then
Cells(RowCount, 6) = Ahead * -1
Ahead = 0
LastColumn = 18
NewColumnCount = 8
For ColumnCount = NewColumnCount To LastColumn Step 2
If Ahead = 0 Then
Cells(RowCount, ColumnCount) = Cells(RowCount + 1, ColumnCount) -
Cells(RowCount + 1, ColumnCount - 2)

ElseIf Ahead > 0 And Ahead > Cells(RowCount + 1, ColumnCount) -
Cells(RowCount + 1, ColumnCount - 2) Then
Cells(RowCount, ColumnCount) = 0
Ahead = Ahead - (Cells(RowCount + 1, ColumnCount) - Cells(RowCount
+ 1, ColumnCount - 2))

ElseIf Ahead > 0 And Ahead < Cells(RowCount + 1, ColumnCount) -
Cells(RowCount + 1, ColumnCount - 2) Then
Cells(RowCount, ColumnCount) = Cells(RowCount + 1, ColumnCount) -
Cells(RowCount + 1, ColumnCount - 2) - Ahead
Ahead = 0

End If
Next ColumnCount
Next RowCount

End Sub
 
R

Richard Marsden

This "IF" construct is missing a final ENDIF:

If Ahead >= 0 Then
Cells(RowCount, 6) = 0
ElseIf Ahead < 0 Then

This kind of error would be easier to pickup if you used tabs more to
reflect the structure of your code. This also makes it easier to match
FOR/NEXT statements and other loops,etc.



Richard
 
W

wfgfreedom

This "IF" construct is missing a final ENDIF:

     If Ahead >= 0 Then
     Cells(RowCount, 6) = 0
     ElseIf Ahead < 0 Then

This kind of error would be easier to pickup if you used tabs more to
reflect the structure of your code. This also makes it easier to match
FOR/NEXT statements and other loops,etc.

Richard

--
Richard Marsden
Winwaed Software Technology LLChttp://www.winwaed.com
Tools and Add-ins for MapPoint -http://www.mapping-tools.com










- Show quoted text -

Thanks for pointing out the simple error, and your advise to prevent
similar errors. I will use tabs more in the future.

Regards,
Frank
 
G

Gary Keramidas

you can also try smart indenter to help you out.

http://www.oaltd.co.uk/Indenter/Default.htm

--


Gary


This "IF" construct is missing a final ENDIF:

If Ahead >= 0 Then
Cells(RowCount, 6) = 0
ElseIf Ahead < 0 Then

This kind of error would be easier to pickup if you used tabs more to
reflect the structure of your code. This also makes it easier to match
FOR/NEXT statements and other loops,etc.

Richard

--
Richard Marsden
Winwaed Software Technology LLChttp://www.winwaed.com
Tools and Add-ins for MapPoint -http://www.mapping-tools.com










- Show quoted text -

Thanks for pointing out the simple error, and your advise to prevent
similar errors. I will use tabs more in the future.

Regards,
Frank
 
A

Alan Beban

I am getting an error message of "Next without For" on the Next
RowCount Statement at the end of the following code. Can anyone tell
me why?

Frank

Sub Macro()

LastRw = 54
NewRowCount = 38
For RowCount = NewRowCount To LastRw Step 2
Ahead = Cells(RowCount, 5) - Cells(RowCount, 4)
If Ahead >= 0 Then
Cells(RowCount, 6) = 0
ElseIf Ahead < 0 Then
Cells(RowCount, 6) = Ahead * -1
Ahead = 0
LastColumn = 18
NewColumnCount = 8
For ColumnCount = NewColumnCount To LastColumn Step 2
If Ahead = 0 Then
Cells(RowCount, ColumnCount) = Cells(RowCount + 1, ColumnCount) -
Cells(RowCount + 1, ColumnCount - 2)

ElseIf Ahead > 0 And Ahead > Cells(RowCount + 1, ColumnCount) -
Cells(RowCount + 1, ColumnCount - 2) Then
Cells(RowCount, ColumnCount) = 0
Ahead = Ahead - (Cells(RowCount + 1, ColumnCount) - Cells(RowCount
+ 1, ColumnCount - 2))

ElseIf Ahead > 0 And Ahead < Cells(RowCount + 1, ColumnCount) -
Cells(RowCount + 1, ColumnCount - 2) Then
Cells(RowCount, ColumnCount) = Cells(RowCount + 1, ColumnCount) -
Cells(RowCount + 1, ColumnCount - 2) - Ahead
Ahead = 0

End If
Next ColumnCount
Next RowCount

End Sub

Indenting discloses that you need an EndIf line after Next ColumnCount.

Alan Beban
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top