VBA OR Function

Last Updated :

-

Edited by :

Reviewed by :

Table Of Contents

arrow

What is OR Function in VBA?

In Excel, logical functions are the heart of daily formulas. Logical functions are there to conduct the logical test and result in Boolean data type, i.e., TRUE or FALSE. Some logical formulas in Excel are IF, IFERROR in excel, ISERROR in excel, , AND, and OR Excel functions.

We hope you have used them quite often as a worksheet function. In VBA, too, we can use all of them, and in this article, we will explain the ways of using the "VBA OR" function.

What is the first thing that comes to mind when you think of the word "OR"?

In simple terms, "OR" means "either this or that."

With the same idea, OR is a logical function that gives the result as TRUE if any of the logical tests is TRUE and gives FALSE if none of the logical tests are TRUE.

It works exactly the opposite of the VBA AND function. The AND function returns TRUE only if all the logical conditions are TRUE. If any of the conditions are not satisfied, we will get FALSE.

VBA OR Function

The formula of VBA OR Function

Let me frame a syntax for you to understand the function.

 OR  OR 

First, we need to mention the logical test, then mention the word "OR," and then mention the second logical test. If you wish to conduct a more logical test, mention the word OR after a logical test.

Of all the logical tests you do, if any of the tests are satisfied or true, then we will get the result as TRUE if none or satisfied, then the result is FALSE.

Examples of Using OR Function in VBA

We will show you a simple example of using the OR function in VBA.

To understand the logical VBA function OR let us give you an example. Let us say we want to conduct the logical test of whether the number 25 is greater than 20 or the number 50 is less than 30.

Step 1: Create a macro name.

Step 2: Define the variable as a string.

Code:

Sub OR_Example1()
 
  Dim i As String

End Sub

Step 3: Now, we will assign the value through the OR logical test for this variable.

Code:

Sub OR_Example1()
 
  Dim i As String

  i =

End Sub

Step 4: Our first logical test is 25 >20.

Code:

Sub OR_Example1()

  Dim i As String

  i = 25 > 20

End Sub

Step 5: Now, after the first logical test, mention the word OR and enter the second logical test.

Code:

Sub OR_Example1()

  Dim i As String

  i = 25 > 20 Or 50 < 30

End Sub

Step 6: Now, VBA OR function tests whether the logical tests are TRUE or FALSE. Now assign the result of the variable to the VBA message box.

Code:

Sub OR_Example1()

  Dim i As String

  i = 25 > 20 Or 50 < 30

  MsgBox i

End Sub

Step 7: Run the Macro and what the result is.

VBA OR Example 1

We got the result as TRUE because out of the two logical tests we have provided, one test is TRUE, so the result is TRUE.

25 is greater than 20, and 50 is not less than 30. In this case, the first logical test is TRUE, but the second is FALSE. Because we have applied the VBA OR function, it needs any one of the conditions to be TRUE to get the result as TRUE.

Now, look at the code below.

Code:

Sub OR_Example1()

  Dim i As String

  i = 25 = 20 Or 50 = 30

  MsgBox i

End Sub

We have changed the logical test equations from > and < to equal (=) sign. Therefore, it will return FALSE as the result because 25 is not equal to 20, and 50 is not equal to 30.

VBA OR Example 1-1

VBA OR Function With IF Condition is Powerful

As we said, the OR function can return either TRUE or FALSE as a result, but with the other logical function, "IF," we can manipulate results as per our needs.

Take the same logical tests from above. Again, the OR function has returned only TRUE or FALSE, but let us combine this OR with IF.

Step 1: Before conducting any test, open the function IF.

Code:

Sub OR_Example2()
  Dim i As String

  IF

End Sub

Step 2: Now, conduct tests using the OR function.

Code:

Sub OR_Example2()
  Dim i As String

  IF 25 = 20 Or 50 = 30

End Sub

Step 3: Put the word “Then” and write the result. If the condition is TRUE, assign the value to the variable as â€śCondition is Satisfied.”

Code:

Sub OR_Example2()
  Dim i As String

  If 25 = 20 Or 50 = 30 Then
    i = "Condition is Satisfied"

End Sub

Step 4: If the condition is FALSE, then we need a different result, so put the word â€śELSE” and, in the next line, assign the value to the variable “what should be the result if the condition or logical test is FALSE.”

Code:

Sub OR_Example2()
  Dim i As String
 
 If 25 = 20 Or 50 = 30 Then
    i = "Condition is Satisfied"
 Else
   i = "Condition is not Satisfied"

End Sub

Step 5: End the IF function with "End If."

Code:

Sub OR_Example2()
  Dim i As String

  If 25 = 20 Or 50 = 30 Then
    i = "Condition is Satisfied"
  Else
    i = "Condition is not Satisfied"
  End If

End Sub

Step 6: Assign the value of the variable result to the message box.

Code:

Sub OR_Example2()
  Dim i As String

  If 25 = 20 Or 50 = 30 Then
     i = "Condition is Satisfied"
  Else
     i = "Condition is not Satisfied"
  End If

  MsgBox i

End Sub

Run the Macro. If the logical test is TRUE, we will get the result as "Condition is Satisfied," or else we will get "Condition is not Satisfied."

VBA OR Example 2

We got the result  â€śCondition is not Satisfied” because both the logical tests are FALSE.

Now, we will change the logical tests.

Code:

Sub OR_Example2()
   Dim i As String

   If 25 > 20 Or 50 < 30 Then
      i = "Condition is Satisfied"
   Else
      i = "Condition is not Satisfied"
   End If

   MsgBox i

End Sub

We will run the macro and see what the result is.

VBA OR Example 2-1

Like this, we can use one logical function with other logical functions to arrive at the results.

Solve the below case study to get used to logical functions.

Case Study to Solve

We have employee names and their respective departments.

Case Study 3

If you have tried and not found the result, then you can refer below code to understand the logic.

Code:

Sub Bonus_Calculation()
  Dim i As Long

  For i = 2 To 10
  If Cells(i, 2).Value = "Finance" Or Cells(i, 2).Value = "IT" Then
     Cells(i, 3).Value = 5000
  Else
     Cells(i, 3).Value = 1000
  End If

  Next i

End Sub

If the employee is from "Finance" or "IT," then they should get the bonus of "5000." For other department employees, the bonus is "1000."

Conduct the logical test and arrive at the results.

Case Study 3-1