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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
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.
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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.
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
It certainly is hard to follow :LOL:

I think that

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

is missing an End If
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows

ADVERTISEMENT

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?
 

ExcelBear

New Member
Joined
Sep 14, 2006
Messages
2
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]
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
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
 

Forum statistics

Threads
1,141,098
Messages
5,704,320
Members
421,338
Latest member
Pepess

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
Top