How to use VBA If Or operator?
The Or operator in VBA is used to check multiple conditions. If any conditions are True, the code inside the If statement executes.
For example:
An example of Or operator in VBA
In this example, we have declared two variables and assigned values.
In the If statement, we checked the values of both variables with the Or operator.
Code:
Sub Or_ex() Dim x, y x = 20 y = 40 If x = 20 Or y = 30 Then MsgBox "One or Both Conditions are True!" End If End Sub
Result:

You can see that the value of x is 20 while y is not equal to 30, still code inside the If statement is executed and it displays the message box.
Using three conditions in the If..Or
Now we have another variable value to test in the If..Or.
If any of the condition is True, it should execute the code inside If and display the message box – otherwise, nothing happens:
Sub Or_ex() Dim x, y, z x = 20 y = 40 z = 60 If x = 20 Or y = 30 Or z = 50 Then MsgBox "One, Two or All Conditions are True!" End If End Sub
Output:

Though the two conditions are False, it still executes the If statement.
Using If..Or with Excel cells values
In this example, we will test the values of two Excel sheet cells.
If any of the conditions is True, it will apply the Blue font color to two rows, otherwise, the red color will be applied:
Sub Or_ex()
Dim prod1 As String, prod2 As String
prod1 = Range("B3").Value
prod2 = Range("B5").Value
If prod1 = "Rice" Or prod2 = "Plates" Then
Range("B3").EntireRow.Font.Color = vbBlue
Range("B5").EntireRow.Font.Color = vbBlue
Else
Range("B3").EntireRow.Font.Color = vbRed
Range("B5").EntireRow.Font.Color = vbRed
End If
End Sub
Result:

You can see, the B3 value is Rice while B5 is Maze, still, it applied a blue color to both rows.
Now, we will check B3 = Wheat and see the output:
Sub Or_ex()
Dim prod1 As String, prod2 As String
prod1 = Range("B3").Value
prod2 = Range("B5").Value
If prod1 = "Wheat" Or prod2 = "Plates" Then
Range("B3").EntireRow.Font.Color = vbBlue
Range("B5").EntireRow.Font.Color = vbBlue
Else
Range("B3").EntireRow.Font.Color = vbRed
Range("B5").EntireRow.Font.Color = vbRed
End If
End Sub
Output:

As both conditions are False, statements in the Else part were executed, making both rows red.