EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home VBA VBA Resources VBA Logical Functions VBA OR
 

VBA OR

Madhuri Thakur
Article byMadhuri Thakur

Updated April 8, 2023

VBA OR

 

 

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:

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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.

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

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.

OR module 1

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

VBA OR Example 1.1

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

Excel VBA OR Example 1.2

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

VBA Example 1.3

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

Example 1.4

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

Example 1.5

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

Example 1.6

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.

VBA OR Example 1.7

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.

OR module 2

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

VBA OR Example 2.1

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

Excel VBA OR Example 2.2

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

Example 2.3

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

Example 2.4

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

Example 2.5

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

Example 2.6

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.

VBA OR Example 2.7

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.

OR module 3

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

VBA OR Example 3.1

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

VBA OR Example 3.2

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

Example 3.3

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

Example 3.4

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

VBA OR Example 3.5

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,

VBA OR Example 4

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.

OR module 4.1

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

Code:

Sub Employee()

End Sub

VBA OR Example 4.2

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

VBA OR Example 4.3

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

VBA OR Example 4.4

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

VBA OR Example 4.5

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 –

  1. VBA On Error
  2. VBA Number Format
  3. VBA VLOOKUP
  4. VBA Function

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

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

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Web Development & many more.

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

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

EDUCBA

Download VBA OR Excel Template

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW