If, elseif, else.... Next without for error

Sphinx404

Board Regular
Joined
May 2, 2015
Messages
186
Office Version
  1. 365
Platform
  1. Windows
Someone please, for the love of God, tell me what my code is missing so I can move on. I keep getting the "NEXT WITHOUT FOR error, and it won't run my macro. I know this has to be a simple fix, but I've been up since 5am... my brain is too fried to look up anymore answers.

Thanks Excel Giants


Code:
Sub Step2()


Dim mycell As Range


For Each mycell In Range("E2", Range("E" & Rows.Count).End(xlUp))


If Len(mycell) = 14 Then
    mycell.Offset(, 16).Value = Right(mycell.Value, 8)
ElseIf Len(mycell) = 21 Then
    mycell.Offset(, 16).Value = "UPS"
ElseIf Len(mycell) = 22 Then
    mycell.Offset(, 16).Value = "UPS"
ElseIf (mycell) <= 7 Then
    mycell.Offset(, 16).Value = "Research"
ElseIf Len(mycell) = 9 Then
    mycell.Offset(, 16).Value = "Unknown"
ElseIf Len(mycell) >= 23 Then
    mycell.Offset(, 16).Value = "Research"
Else
    mycell.Offset(, 16).Value = ""


Next mycell


End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
This code works, but I was trying to simplify it with the code version I posted above.

Code:
Sub Step2()


Dim mycell As Range


For Each mycell In Range("E2", Range("E" & Rows.Count).End(xlUp))


If Len(mycell) = 14 Then
    mycell.Offset(, 16).Value = Right(mycell.Value, 8)
End If


If Len(mycell) = 21 Then
    mycell.Offset(, 16).Value = "UPS"
End If


If Len(mycell) = 22 Then
    mycell.Offset(, 16).Value = "UPS"
End If


If Len(mycell) <= 7 Then
    mycell.Offset(, 16).Value = "Research"
End If


If Len(mycell) = 9 Then
    mycell.Offset(, 16).Value = "Unknown"
End If


If Len(mycell) >= 23 Then
    mycell.Offset(, 16).Value = "Research"
End If

Next mycell

End Sub
 
Last edited:
Upvote 0
Perhaps a Select Case?
Code:
Dim mycell As Range


    For Each mycell In Range("E2", Range("E" & Rows.Count).End(xlUp))

        Select Case Len(mycell)
            Case 14
                mycell.Offset(, 16).Value = Right(mycell.Value, 8)
            Case 21, 22
                mycell.Offset(, 16).Value = "UPS"
            Case Is <= 7
                mycell.Offset(, 16).Value = "Research"
            Case 9
                mycell.Offset(, 16).Value = "Unknown"
            Case Is >= 23
                mycell.Offset(, 16).Value = "Research"
        End Select

    Next mycell
 
Upvote 0
Your original post has a else statement but your second script did no have a else statement.

If you need no else statement use this:
Code:
Sub Step2()
Dim mycell As Range
For Each mycell In Range("E2", Range("E" & Rows.Count).End(xlUp))
If Len(mycell) = 14 Then mycell.Offset(, 16).Value = Right(mycell.Value, 8)
If Len(mycell) = 21 Then mycell.Offset(, 16).Value = "UPS"
If Len(mycell) = 22 Then mycell.Offset(, 16).Value = "UPS"
If (mycell) <= 7 Then mycell.Offset(, 16).Value = "Research"
If Len(mycell) = 9 Then mycell.Offset(, 16).Value = "Unknown"
If Len(mycell) >= 23 Then mycell.Offset(, 16).Value = "Research"
Next mycell
End Sub
 
Upvote 0
... and just so you understand the reason your code in post number 1 errors is that you are missing an End If above the Next mycell line.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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