Next without for error msg & I have end if

omj2100

New Member
Joined
Feb 23, 2012
Messages
1
I am stumped...any ideas why i get the "Next without for" error message?
checkstructure is the name of the function.
My suspicion is the "Exit For" in the Else statement is not allowed?

Code:
Dim STVal As String
    STVal = "ST050"
Dim i As Integer
Dim myanswer As Double
Dim myCheckVal As Boolean

For i = 1 To 5
    On Error Resume Next
        myanswer = WorksheetFunction.Search(STVal & Chr(i), testname, 1)
        If Err.Number = xlErrValue Then
        Next i
        Else
            checkstructure = True
            Exit For
        End If
        
checkstructure = False
End Function
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
here is a great reason to indent your code properly...

you have crossed your if structure with your for next loop.

Code:
    Dim STVal As String
    STVal = "ST050"
    Dim i As Integer
    Dim myanswer As Double
    Dim myCheckVal As Boolean

    For i = 1 To 5
        On Error Resume Next
        myanswer = WorksheetFunction.Search(STVal & Chr(i), testname, 1)
        If Err.Number = xlErrValue Then

        Else
            checkstructure = True
            Exit For
        End If
    Next i
        
    checkstructure = False
End Function
 
Upvote 0

Forum statistics

Threads
1,214,874
Messages
6,122,036
Members
449,062
Latest member
mike575

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