VBA IF Statement Combined with AND + OR

rsmeyerson

Board Regular
Joined
Nov 29, 2014
Messages
104
Hello,

I'm having trouble getting the 'AND' portion to work in the example code below. My issue is that FORMULA1 is being inserted into Column S when there is a Column J 'OR' match but not a Column E "NEW" match. Any help is greatly appreciated.

VBA Code:
With Sheets("Retail")  
   
    Last = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = Last To 2 Step -1

        If (.Cells(i, "E").Value) = "NEW" And (.Cells(i, "J").Value) Like "*xxx*" Or (.Cells(i, "J").Value) Like "*yyy*" Or (.Cells(i, "J").Value) Like "*zzz*" Then
            .Cells(i, "S").Value = "FORMULA1"
        Else
            .Cells(i, "S").Value = "FORMULA2"
        End If
   
     Next i
End With
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
How about:
VBA Code:
        If .Cells(i, "E").Value = "NEW" And (.Cells(i, "J").Value Like "*xxx*" Or .Cells(i, "J").Value Like "*yyy*" Or .Cells(i, "J").Value Like "*zzz*") Then
Be aware that by default the Like operator is case-sensitive, so Like "*xxx*" matches xxx, but not XXX. If you have Option Compare Text at the top of the module then Like is case-insensitive.
 
Upvote 0
Solution
You could also add another IF statement to separate the condition like this:

VBA Code:
Option Explicit
Sub Macro1()

    Dim Last As Long, i As Long
    
    Application.ScreenUpdating = False

    With Sheets("Retail")
        Last = .Cells(.Rows.Count, "A").End(xlUp).Row
        For i = Last To 2 Step -1
            If .Cells(i, "E").Value = "NEW" Then
                If .Cells(i, "J").Value Like "*xxx*" Or .Cells(i, "J").Value Like "*yyy*" Or .Cells(i, "J").Value Like "*zzz*" Then
                    .Cells(i, "S").Value = "FORMULA1"
                Else
                    .Cells(i, "S").Value = "FORMULA2"
                End If
            End If
         Next i
    End With
    
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
How about:
VBA Code:
        If .Cells(i, "E").Value = "NEW" And (.Cells(i, "J").Value Like "*xxx*" Or .Cells(i, "J").Value Like "*yyy*" Or .Cells(i, "J").Value Like "*zzz*") Then
Be aware that by default the Like operator is case-sensitive, so Like "*xxx*" matches xxx, but not XXX. If you have Option Compare Text at the top of the module then Like is case-insensitive.
Thank you very much for your help. The parentheses after and solved the problem. Thanks again!
 
Upvote 0

Forum statistics

Threads
1,215,421
Messages
6,124,806
Members
449,191
Latest member
rscraig11

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