if & Or statement in VBA.

albert211994

New Member
Joined
Feb 7, 2017
Messages
10
Here are my code:
Code:
Sub GL()With Application
    .EnableEvents = False
    .ScreenUpdating = False
    End With
    
    Dim i As Integer
    For i = 2 To 20000
    
        If Trim(ThisWorkbook.Sheets("FRA&FFC").Cells(i, 6).Text) <> "" Then
            If Trim(ThisWorkbook.Sheets("FRA&FFC").Cells(i, 6).Text) = "Z002" Then
                If (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200290" Or _
                (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200410" Or _
                (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200422" Or _
                (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200469" Or _
                (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200748" Then
                    ThisWorkbook.Sheets("FRA&FFC").Cells(i, 18).Value = "40120002"
                Else:
                    ThisWorkbook.Sheets("FRA&FFC").Cells(i, 18).Value = "40120001"
            End If
            
            If Trim(ThisWorkbook.Sheets("FRA&FFC").Cells(i, 6).Text) = "Z003" Then
                ThisWorkbook.Sheets("FRA&FFC").Cells(i, 18).Value = "40120007"
            End If
        End If
    Next i
    
    'MsgBox "Done"
End Sub

I'm getting an error "Next without For".
Can any body help me with regards with this error. Why I am getting this?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Sometimes Excel VBA error messages through up red herrings. You are actually missing and "End If" statement (there should be one for every "If")
Code:
Sub GL()

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    Dim i As Integer
    
    For i = 2 To 20000
    
        If Trim(ThisWorkbook.Sheets("FRA&FFC").Cells(i, 6).Text) <> "" Then
            If Trim(ThisWorkbook.Sheets("FRA&FFC").Cells(i, 6).Text) = "Z002" Then
                If (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200290" Or _
                    (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200410" Or _
                    (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200422" Or _
                    (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200469" Or _
                    (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200748" Then
                        ThisWorkbook.Sheets("FRA&FFC").Cells(i, 18).Value = "40120002"
                Else:
                    ThisWorkbook.Sheets("FRA&FFC").Cells(i, 18).Value = "40120001"
                [B][COLOR=#ff0000]End If[/COLOR][/B]
            End If
            
            If Trim(ThisWorkbook.Sheets("FRA&FFC").Cells(i, 6).Text) = "Z003" Then
                ThisWorkbook.Sheets("FRA&FFC").Cells(i, 18).Value = "40120007"
            End If
            
        End If
        
    Next i
    
    'MsgBox "Done"
End Sub
 
Upvote 0
Sometimes Excel VBA error messages through up red herrings. You are actually missing and "End If" statement (there should be one for every "If")
Code:
Sub GL()

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    
    Dim i As Integer
    
    For i = 2 To 20000
    
        If Trim(ThisWorkbook.Sheets("FRA&FFC").Cells(i, 6).Text) <> "" Then
            If Trim(ThisWorkbook.Sheets("FRA&FFC").Cells(i, 6).Text) = "Z002" Then
                If (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200290" Or _
                    (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200410" Or _
                    (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200422" Or _
                    (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200469" Or _
                    (ThisWorkbook.Sheets("FRA&FFC").Cells(i, 5).Text) = "200748" Then
                        ThisWorkbook.Sheets("FRA&FFC").Cells(i, 18).Value = "40120002"
                Else:
                    ThisWorkbook.Sheets("FRA&FFC").Cells(i, 18).Value = "40120001"
                [B][COLOR=#ff0000]End If[/COLOR][/B]
            End If
            
            If Trim(ThisWorkbook.Sheets("FRA&FFC").Cells(i, 6).Text) = "Z003" Then
                ThisWorkbook.Sheets("FRA&FFC").Cells(i, 18).Value = "40120007"
            End If
            
        End If
        
    Next i
    
    'MsgBox "Done"
End Sub

I missed that end if. Thanks for pointing that out.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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