## Excel VBA OR Function

Like a worksheet function excel VBA also has a logical function which is OR function. In any programming language OR function is defined as follows:

Condition 1 OR Condition 2. If any of the given conditions happens to be true the value returned by the function is true while if both of the condition happens to be false the value returned by the function is false. OR Function can be termed as that it is opposite to AND function because in AND function both of the condition needs to be true in order to get a true value. Even if a single condition is termed as false then the whole value returned by the AND function is false. While in OR Function only one condition needs to be true in order to get TRUE as an output.

**Syntax of OR Function in Excel VBA**

VBA OR function has the following syntax:

{Condition 1} OR {Condition 2}

Let us use this function in VBA to have a clear mindset of how to use this function in general terms.

**Note**: In order to use VBA we need to have developer access enabled from the file tab.### How to Use Excel VBA OR Function?

We will learn how to use VBA OR Function with few examples in excel.

#### Example #1 – VBA OR

To use this OR Function in our first example let us assume that there are four values A, B, C and D. We will assign these variables certain Values and check that if A>B or C>D and if any of the conditions is true what we will get as an output.

Follow the below steps to use VBA Union function in Excel:

**Step 1:** Now once we are in VB Editor go ahead and insert a new module from the insert section.

**Step 2:** A code window will appear on the right-hand side of the screen. Define the subfunction as Sample.

**Code:**

Sub Sample() End Sub

**Step 3:** Define the four variables A B C and D as integers.

**Code:**

Sub Sample() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer End Sub

**Step 4:** Define a variable X to store the value of OR Function, define it as a string.

**Code:**

Sub Sample() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer Dim X As String End Sub

**Step 5:** Assign Random Values to A B C and D.

**Code:**

Sub Sample() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer Dim X As String A = 10 B = 15 C = 20 D = 25 End Sub

**Step 6:** Define X’s Values as conditions for A B C and D.

**Code:**

Sub Sample() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer Dim X As String A = 10 B = 15 C = 20 D = 25 X = A > B Or C > D End Sub

**Step 7:** Now we will display the value of X stored in it.

**Code:**

Sub Sample() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer Dim X As String A = 10 B = 15 C = 20 D = 25 X = A > B Or C > D MsgBox X End Sub

**Step 8:** Run the code from the run button provided in the screenshot below and then we see the following result when we run the above code.

Why we get the value as false because A is not greater than B and C is not greater than D. Both of the values of the condition were returned as false so our final output is also returned as false.

#### Example #2 – VBA OR

Now let us interchange the values for X from example 1. I mean to say that this time our expression for X will be A<B OR C>D. And we will see what will be the result displayed by the code.

**Step 1:** Now once we are in VB Editor go ahead and insert a new module from the insert section.

**Step 2:** A code window will appear on the right-hand side of the screen. Define the subfunction as Sample1.

**Code:**

Sub Sample1() End Sub

**Step 3:** Define the four variables A B C and D as integers.

**Code:**

Sub Sample1() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer End Sub

**Step 4:** Define a variable X to store the value of OR Function, define it as a string.

**Code:**

Sub Sample1() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer Dim X As String End Sub

**Step 5:** Assign Random Values to A B C and D.

**Code:**

Sub Sample1() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer Dim X As String A = 10 B = 15 C = 20 D = 25 End Sub

**Step 6:** Define X’s Values as conditions for A B C and D.

**Code:**

Sub Sample1() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer Dim X As String A = 10 B = 15 C = 20 D = 25 X = A < B Or C > D End Sub

**Step 7:** Now we will display the value of X stored in it.

**Code:**

Sub Sample1() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer Dim X As String A = 10 B = 15 C = 20 D = 25 X = A < B Or C > D MsgBox X End Sub

**Step 8**: Run the code above from the run button as shown and we will see the following result as we run the above code.

Why we get the value as True because A is less than B and C is not greater than D. One of the values of the condition were returned as true so our final output is also returned as true.

#### Example #3 – VBA OR

Now let us use OR Function in VBA with the IF function. Earlier we used another variable to store the Boolean value of OR function and display it. This time we will use a personalized message to display using or and if function.

**Steps 1:** Now once we are in VB Editor go ahead and insert a new module from the insert section.

**Step 2:** A code window will appear on the right-hand side of the screen. Define the subfunction as Sample2.

**Code:**

Sub Sample2() End Sub

**Step 3:** Define all the four variables A B C and D as integers and assign them random values.

**Code:**

Sub Sample2() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer A = 5 B = 10 C = 15 D = 20 End Sub

**Step 4:** Now write the if statement for the given variables, for example, like in the code given below,

**Code:**

Sub Sample2() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer A = 5 B = 10 C = 15 D = 20 If (A < B) Or (C > D) Then End Sub

**Step 5:** Write a personalized message if any of the logical conditions is true or even if it is false.

**Code:**

Sub Sample2() Dim A As Integer Dim B As Integer Dim C As Integer Dim D As Integer A = 5 B = 10 C = 15 D = 20 If (A < B) Or (C > D) Then MsgBox "One of the conditions is true" Else MsgBox "None of the conditions is true" End If End Sub

**Step 6:** Run the above code from the run button and we will get the following result displayed.

As one of the conditions were true we have the above result.

#### Example #4 – VBA OR

Let use VBA OR function in a real scenario. We have the following data, Name of the employees and sales done by them. If their sales are equals to specific criteria or greater than that then they will receive the incentive or there will be no incentive for those employees. Have a look at the data below,

The incentive criteria are 10000 for this example. If the sales done by the employees is equals to or above 10000 they will receive the incentive.

**Steps 1:** Now once we are in VB Editor go ahead and insert a new module from the insert section.

**Step 2:** In the code window, declare the subfunction,

**Code:**

Sub Employee() End Sub

**Step 3:** Declare a variable X as Long and write the if statement as below,

**Code:**

Sub Employee() Dim X As Long For X = 2 To 10 If Cells(X, 2).Value = 10000 Or Cells(X, 2).Value > 10000 Then Cells(X, 3).Value = "Incentive" Else Cells(X, 3).Value = "No Incentive" End If End Sub

**Step 4:** Start the loop for the next cell.

**Code:**

Sub Employee() Dim X As Long For X = 2 To 10 If Cells(X, 2).Value = 10000 Or Cells(X, 2).Value > 10000 Then Cells(X, 3).Value = "Incentive" Else Cells(X, 3).Value = "No Incentive" End If Next X End Sub

**Step 5:** Run the code to form the run button provided and once we have run the code check the result below,

In the If Statement, we used that if the sales are done is equals to 10000 or sales done greater than 10000 the employee will receive incentive.

### Things to Remember

There are a few things we need to remember about VBA OR Function:

- It is a logical function in excel or any other programming language.
- It returns a logical output true or false.
- It is the opposite of AND Function.

### Recommended Articles

This is a guide to VBA OR. Here we have discussed how to use Excel VBA OR Function along with practical examples and downloadable excel template. You can also go through our other suggested articles –