Or is a logical function in any of the programming languages and similar in VBA we have OR function, as it is a logical function the result given by this function is either true or false, this function is used for two or many conditions together and gives us true result when either of the conditions is returned true.

## What is OR Function in VBA?

In excel logical functions is the heart of the formulas we use on a daily basis. Logical functions are there to conduct the logical test and gives result in Boolean data type i.e. either TRUE or FALSE. Some of the logical formulas in excel are “IF, IFERROR, ISERROR, AND, and OR excel function”. I 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 to you the ways of using “VBA OR” function.

What is the first thing comes to your 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 which gives the result as TRUE if any one of the logical tests is TRUE and gives FALSE as the result if none of the logical tests are TRUE.

This works exactly opposite of VBA AND function. AND function returns TRUE only if all the logical conditions are TRUE, if any one of the condition is not satisfied then we will get FALSE as the result.

### Formula of VBA OR Function

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

[Logical Test] OR [Logical Test] OR [Logical Test]

First, we need to mention what is the logical test then mention the word OR then mention what is the second logical test. If you wish to conduct more logical test then mention the word OR after everting logical test.

Off all the logical tests you do, if any one of the tests is 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 me give you an example. Let’s say we want to conduct the logical test whether the number 25 is greater than 20 or 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 for this variable, we will assign the value through the OR logical test.

**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: **Ok, 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.

We got the result as TRUE because out of 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 second is FALSE. Because we have applied the VBA OR function, it needs any one of the condition to be TRUE to get the result as TRUE.

Now, look at the below code.

**Code:**

Sub OR_Example1() Dim i As String i = 25 = 20 Or 50 = 30 MsgBox i End Sub

I have changes the logical test equations from > and < to equal (=) sign. This will return FALSE as the result because 25 is not equal to 20 and 50 is not equal to 30.

#### VBA OR Function With IF Condition is Powerful

As I told OR can return either TRUE or FALSE as the result, but with the other logical function “IF”, we can manipulate results as per our needs.

Take the same logical tests from above, OR has returned only TRUE or FALSE but let’s 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 the 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 the word **“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”.

We got the result as **““Condition is not Satisfied”** because both the logical tests are FALSE.

Now I 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

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

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

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

#### Case Study to Solve

I have employee names and their respective departments.

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 as “5000”. For other department employees bonus is “1000”.

Conduct the logical test and arrive the results.

### Recommended Articles

This has been a guide to VBA OR Function. Here we learn how to use OR Logical Operator in VBA along with some practical examples and downloadable excel template. Below are some useful excel articles related to VBA –

- VBA Boolean
- Data Type in VBA Excel
- Not Equal in VBA
- IFERROR in VBA
- VBA On Error
- VBA Code in Excel
- VBA UserForm
- VBA Match

- 3 Courses
- 12 Hands-on Projects
- 43+ Hours
- Full Lifetime Access
- Certificate of Completion