VBA IF OR

IF OR are not a single statement these are two logical functions which are used to together some times in VBA, we use these two logical functions together when we have more than one criteria to check with and if any one of the criteria is fulfilled we get the true result, when we use the if statement Or statement is used between the two criteria’s of If statement.

IF OR Function in VBA

Logical functions are the heart of any criteria based calculations. “IF” is the most popular logical function, be it as a worksheet function or as a VBA function, it serves excellently for our needs. But one more logical function, “OR” in excel, is the most underrated function. It is also important to master when it comes to solving complex calculations. In this article, we will take you through the VBA IF OR Function in detail. Read the full article to get the function in detail.

VBA IF OR

You are free to use this image on your website, templates etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA IF OR (wallstreetmojo.com)

How to Use IF with OR Function in VBA?

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

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

A combination of logical functions is the best pairs in excel. When you combine many logical formulas inside the other logical formula, it suggests that calculation requires many conditions to test.

Now, look at the syntax of the IF OR function in VBA.

[Test] OR [Test] OR [Test]

This is the same as we saw in the worksheet example. For a better understanding, look at the below example.

VBA IF OR Example 1

We have the previous month’s price, the last 6-month average price, and the current monthly price here.

To decide on whether to buy the product or not, we need to do some tests here, and those tests are.

If the Current Price is less than or equal to any of one of the other two prices, we should get the result as “Buy” or else should get the result as “Do Not Buy.”

Step 1: Open IF condition inside the subprocedure.

Code:

Sub IF_OR_Example1()

 If

End Sub
VBA IF OR Example 1-1

Step 2: Inside the IF condition, apply the first logical test as Range(“D2”).Value <= Range(“B2”).Value

Code:

Sub IF_OR_Example1()

 If Range(“D2”).Value <= Range(“B2”).Value

End Sub
VBA IF OR Example 1-2

Step 3: First logical condition is done, now open OR statement.

Code:

Sub IF_OR_Example1()

 If Range("D2").Value <= Range("B2").Value OR

End Sub
VBA IF OR Example 1-3

Step 4: Now apply the second logical condition as Range(“D2”).Value <= Range(“C2”).Value

Code:

Sub IF_OR_Example1()

 If Range("D2").Value <= Range("B2").Value OR Range("D2").Value <= Range("C2").Value

End Sub
VBA IF OR Example 1-4

Step 5: Ok, we are done with the logical tests here. After the logical tests, put the word “Then.”

Code:

Sub IF_OR_Example1()

 If Range("D2").Value <= Range("B2").Value Or Range("D2").Value <= Range("C2").Value Then

End Sub
VBA IF OR Example 1-5

Step 6: In the next line, write what should be the result if the logical testLogical TestA logical test in Excel results in an analytical output, either true or false. The equals to operator, “=,” is the most commonly used logical test.read more is TRUE. If the condition is TRUE, we need the result as “Buy” in cell E2.

Code:

Sub IF_OR_Example1()

 If Range("D2").Value <= Range("B2").Value Or Range("D2").Value <= Range("C2").Value Then  

    Range("E2").Value = "Buy"

End Sub
VBA IF OR Example 1-6

Step 7: If the result is FALSE, we should get the result as “Do Not Buy.” So in the next line, put “Else” and write the code in the next line.

Code:

Sub IF_OR_Example1()

If Range("D2").Value <= Range("B2").Value Or Range("D2").Value <= Range("C2").Value Then 

   Range("E2").Value = "Buy"
Else
   Range("E2").Value = "Do Not Buy"

End Sub
VBA IF OR Example 1-7

Step 8: Close the IF statement with the word “End If.”

Code:

Sub IF_OR_Example1()

If Range("D2").Value <= Range("B2").Value Or Range("D2").Value <= Range("C2").Value Then

   Range("E2").Value = "Buy"
Else
   Range("E2").Value = "Do Not Buy"
End If

End Sub
VBA IF OR Example 1-8

Ok, we are done with the coding part.

Let’s run this code using F5 or manually through the run option and see what the result is in cell E2.

Example 1-9

We got the result as “Buy” because the current monthly price of Apple is less than the price of both “Previous Month” as well as “6 Month Average Price”.

IF OR VBA Function with Loops (Advanced)

Once you understand the formula, try to use it with a larger number of cells. In the case of a larger number of cells, we cannot write any line of code, so we need to use VBA loopsUse VBA LoopsLoops are commonly used in all of the programming languages, where there is a certain need or a criteria when we need a certain code to run a certain times. In VBA, there are several different types of loops, including Do while, Do till, For Loop, and For each loop. These loops help code to execute until the condition is met.read more.

For the above set of data, I have added a few more lines.

Example 2

We need to use the For Next loop here.

Just keep the current code as it is.

Declare the variable as an integer.

Example 2-1

Now open For Next Loop from 2 to 9.

Example 2-2

Now, wherever we have cell referenceCell ReferenceCell reference in excel is referring the other cells to a cell to use its values or properties. For instance, if we have data in cell A2 and want to use that in cell A1, use =A2 in cell A1, and this will copy the A2 value in A1.read more, change the current number, and concatenate the variable “k” with them.

For example, Range (“D2”).Value should be Range (“D” & k).Value

Example 2-3

Now run the code. We should get the status in all the cells.

Example 2-4

You can copy the code below.

Code:

Sub IF_OR_Example1()

Dim k As Integer

For k = 2 To 9

If Range("D" & k).Value <= Range("B" & k).Value Or Range("D" & k).Value <= Range("C" & k).Value Then

Range("E" & k).Value = "Buy"
Else

Range("E" & k).Value = "Do Not Buy"

End If

Next k

End Sub

Recommended Articles

This has been a guide to VBA IF OR. Here we learn how to use If Condition with Or Function in Excel VBA along with examples & downloadable templates. Below are some useful articles related to VBA –

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