WallStreetMojo

WallStreetMojo

WallStreetMojo

MENUMENU
  • Free Tutorials
  • Certification Courses
  • Excel VBA All in One Bundle
  • Login
Home » Excel, VBA & Power BI » Learn VBA » VBA OR Function

VBA OR Function

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 are 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 in excel, ISERROR in excel, 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 the “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 that 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 anyone of the conditions is not satisfied, then we will get FALSE as a result.

VBA OR Function

The 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 the second logical test is. If you wish to conduct a more logical test, then mention the word OR after everting a logical test.

Of all the logical tests you do, if anyone 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.

You can download this VBA OR Excel Template here – VBA OR Excel Template

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.

Popular Course in this category
Sale
VBA Training (3 Courses, 12+ Projects)
4.6 (247 ratings)
3 Courses | 12 Hands-on Projects | 43+ Hours | Full Lifetime Access | Certificate of Completion
View Course

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.

VBA OR Example 1

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 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 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 Example 1-1

VBA OR Function With IF Condition is Powerful

As I told, OR 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, 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 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 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.”

VBA OR Example 2

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.

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

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

Conduct the logical test and arrive at the results.

Case Study 3-1

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 a downloadable excel template. Below are some useful excel articles related to VBA –

  • IFERROR in VBA
  • Excel VBA On Error Statement
  • Excel OR Function
  • VBA Month Function
0 Shares
Share
Tweet
Share
VBA Training (3 Courses, 12+ Projects)
  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>
Primary Sidebar
Footer
COMPANY
About
Reviews
Contact
Privacy
Terms of Service
RESOURCES
Blog
Free Courses
Free Tutorials
Investment Banking Tutorials
Financial Modeling Tutorials
Excel Tutorials
Accounting Tutorials
Financial Statement Analysis
COURSES
All Courses
Financial Analyst All in One Course
Investment Banking Course
Financial Modeling Course
Private Equity Course
Venture Capital Course
Excel All in One Course

Copyright © 2021. CFA Institute Does Not Endorse, Promote, Or Warrant The Accuracy Or Quality Of WallStreetMojo. CFA® And Chartered Financial Analyst® Are Registered Trademarks Owned By CFA Institute.
Return to top

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Book Your One Instructor : One Learner Free Class
Let’s Get Started
Please select the batch
Saturday - Sunday 9 am IST to 5 pm IST
Saturday - Sunday 9 am IST to 5 pm IST

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

WallStreetMojo

Free Excel Course

Excel functions, Formula, Charts, Formatting creating excel dashboard & others

* Please provide your correct email id. Login details for this Free course will be emailed to you

Login

Forgot Password?

WallStreetMojo

Download VBA OR Excel Template

New Year Offer - VBA Training Course (6 courses, 35+ hours video) View More