Compile Error: "Next without For"... not true!

ExcelBear

New Member
Joined
Sep 14, 2006
Messages
2
Hi, I get the following error message with the below code, but it's not true, there IS a "For" statement. Where am I going wrong?? Sorry, forum has lost all my indent formatting. Thanks so much!

---------------------------
Microsoft Visual Basic
---------------------------
Compile error:

Next without For
---------------------------
OK Help
---------------------------


----------------------------------------------------------------

Option Explicit

Private Sub CommandButton1_Click()

Dim fl As Boolean, r As Long, i, j, t, n, k, m, p, c As Integer, tr, br As String

fl = False
c = 0
m = Cells(3, 4).Value

Do Until fl = True
For i = 13 To 1000 'count trades

If Cells(i, 1) = "" Then fl = True
If Cells(i, 1) <> "" Then c = c + 1
Next i

Loop

fl = False ' reset fl for next count
i = 0


If OptionButton1.Value = True Then
'begin code for special assignment
MsgBox "There are " & Str(c) & " trades on tracking."

' excluded trades
Do Until fl = True

For i = 13 To c + 13
If Cells(i, 2).Value = "" Then fl = True

If Cells(i, 9) = False Then Cells(i, 6).Value = "EXCLUDED"


Next i
Loop
fl = False

'assignment


For i = 13 To 13 + c


If Cells(i, 13) <> "ignore" Then

p = Cells(i, 13)

If Cells(i, 10) = Worksheets(3).Cells(p, 1) Then

For n = 0 To 9

For k = 3 To 10

If Cells(i, 10) = Worksheets(3).Cells(p + n, 1) And Cells(k, 6) = Worksheets(3).Cells(p + n, 3) Then

If Cells(i, 6) <> "EXCLUDED" Then Cells(i, 6) = Cells(k, 6)
End If


Next k

Next n

End If





Next i





' end code for special assignment
ElseIf OptionButton2.Value = True Then

End If




End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Well it is sort of true, but the error message is actually pretty misleading.

What the problem is at first glance is that you've missed an End If somewhere.

Hard to tell where because the loss of indentation.

To remedy that enclose the code in code tags by:

1 Hitting Preview not Submit.

2 Select all the indented code.

3 Press the Code button.

4 Now hit Submit.
 
Upvote 0
Hard to follow that (when you post code, it helps if you put it in code tags), but move your Next statements outside of your If statements.
 
Upvote 0
It certainly is hard to follow :LOL:

I think that

Code:
If Cells(i, 13) <> "ignore" Then

is missing an End If
 
Upvote 0
Yeah, it looks like there are at least 2 problems, the End If
and the Next contained within the If:

This section:

Code:
For n = 0 To 9

For k = 3 To 10

If Cells(i, 10) = Worksheets(3).Cells(p + n, 1) And Cells(k, 6) = Worksheets(3).Cells(p + n, 3) Then

If Cells(i, 6) <> "EXCLUDED" Then Cells(i, 6) = Cells(k, 6)
End If


Next k

Next n

End If

The For Statement is outside the If, but the Next is within it.

What is this code supposed to do?
 
Upvote 0
thanks for your replies. Code is looking up a list of trades, then cycling through a list of reviewers until a participating reviewer is found. Code is:


Code:
Dim fl As Boolean, r As Long, i, j, t, n, k, m, p, c As Integer, tr, br As String


fl = False
c = 0
m = Cells(3, 4).Value

Do Until fl = True
    For i = 13 To 25  'must change!!!!!!!!
    
        If Cells(i, 1) = "" Then fl = True
        If Cells(i, 1) <> "" Then c = c + 1
        Next i
        
    Loop
    
fl = False ' reset fl for next count
i = 0


    If OptionButton1.Value = True Then
    'begin code for special assignment
    MsgBox "There are " & Str(c) & " trades on tracking."
       
            ' excluded trades
            Do Until fl = True
            
               For i = 13 To c + 13
                  If Cells(i, 2).Value = "" Then fl = True
                      
                  If Cells(i, 9) = False Then Cells(i, 6).Value = "EXCLUDED"
                        
                
              Next i
           Loop
                        fl = False
                
           'assignment
           
           
            For i = 13 To 13 + c
                
                                                            
                    If Cells(i, 13) <> "ignore" Then
      
                        p = Cells(i, 13)
                                                
                                    If Cells(i, 10) = Worksheets(3).Cells(p, 1) Then
                                                
                                For n = 0 To 9
                                 
                                    For k = 3 To 10
                                                                             
                                        If Cells(i, 10) = Worksheets(3).Cells(p + n, 1) And Cells(k, 6) = Worksheets(3).Cells(p + n, 3) Then
                                        
                                            If Cells(i, 6) <> "EXCLUDED" Then Cells(i, 6) = Cells(k, 6)
                                        End If
                                    
                                                                                                                        
                                    Next k
                                                                             
                                Next n
                                 
                    End If
           
                
            Next i
' end code for special assignment
ElseIf OptionButton2.Value = True Then

End If




End Sub[/code]
 
Upvote 0
This compiles but I can't tell you if it will actually do what you want.
Code:
Private Sub CommandButton1_Click()
Dim fl As Boolean, r As Long, i, j, t, n, k, m, p, c As Integer, tr, br As String

    fl = False
    c = 0
    m = Cells(3, 4).Value
    
    Do Until fl = True
        For i = 13 To 25  'must change!!!!!!!!
            If Cells(i, 1) = "" Then fl = True
            If Cells(i, 1) <> "" Then c = c + 1
        Next i
    Loop
       
    fl = False ' reset fl for next count
    i = 0
    If OptionButton1.Value = True Then
        'begin code for special assignment
        MsgBox "There are " & Str(c) & " trades on tracking."
           
        ' excluded trades
        Do Until fl = True
            For i = 13 To c + 13
                If Cells(i, 2).Value = "" Then fl = True
                If Cells(i, 9) = False Then Cells(i, 6).Value = "EXCLUDED"
            Next i
        Loop
        fl = False
                   
        'assignment
        For i = 13 To 13 + c
             If Cells(i, 13) <> "ignore" Then
                p = Cells(i, 13)
                If Cells(i, 10) = Worksheets(3).Cells(p, 1) Then
                    For n = 0 To 9
                        For k = 3 To 10
                            If Cells(i, 10) = Worksheets(3).Cells(p + n, 1) And Cells(k, 6) = Worksheets(3).Cells(p + n, 3) Then
                                If Cells(i, 6) <> "EXCLUDED" Then Cells(i, 6) = Cells(k, 6)
                            End If
                        Next k
                    Next n
                End If
            End If
        Next i
     ' end code for special assignment
    ElseIf OptionButton2.Value = True Then
    
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,213,496
Messages
6,113,995
Members
448,539
Latest member
alex78

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