Table Of Contents
Excel VBA IIF
If you are a regular user of VBA macros, you must have come across the function called "IIF," or you might have seen this function on the internet. At first sight, you must have thought it is an IF condition like our regular IF statement in Excel. But this is not the same IF statement we use to evaluate logical tests and arrive at results based on our criteria. Therefore, this article will take you through the "VBA IIF" condition in VBA.
What Does IIF Condition Do in VBA?
It is very similar to our IF condition but slightly different. For example, the "VBA IIF" condition tests the supplied expression or logical test and returns either TRUE or FALSE.
VBA IIF Syntax
Take a look at the syntax of the IIF function.
- Expression: This is nothing but the logical test we would like to conduct.
- Ture Part: If the logical test is TRUE, what should be the TRUE part?
- False Part: If the logical test is FALSE, what should be the result of the FALSE part?
We can enter our results with TRUE and FALSE parts. Though arguments look similar to the IF condition, this will be slightly different. We will see that in the examples of the Excel VBA IIF function.
One of the key differences between regular "IF" and this "IIF" is we can reduce the code to a single line with where IF condition, and it takes a minimum of 5 lines to arrive at the same result.
Example of VBA IIF Function
Below are examples of the VBA IIF function in Excel.
Example #1 - VBA IIF
We will see one simple example of the IIF function. Now, we will test whether one number is greater than or less than another number. Then, follow the below steps to write the VBA code.
Step 1: Start the macro.
Step 2: Define the variable as String in VBA.
Code:
Sub IIF_Example() Dim FinalResult As String End Sub
Step 3: Define two more variables as Long in VBA.
Code:
Sub IIF_Example() Dim FinalResult As String Dim Number1 As Long Dim Number2 As Long End Sub
Step 4: Now, for the variable "Number1," assign the value of 105, and for variable "Number2," assign the value of 100.
Code:
Sub IIF_Example() Dim FinalResult As String Dim Number1 As Long Dim Number2 As Long Number1 = 105 Number2 = 100 End Sub
Step 5: Now, for the first defined variable, "FinalResult," we will assign the result of the IIF function. So open the IIF for the variable.
Step 6: Supply the expression as Number1 > Number2.
Step 7: What should be the result if the expression is TRUE? We will assign the result as "Number 1 is greater than Number 2."
Step 8: What should be the result if the expression is FALSE? We will assign the result as "Number 1 is less than Number 2."
Now the variable value will be either one of the below.
If True: “Number 1 is greater than Number 2.”
If False: “Number 1 is less than Number 2.”
Step 9: Let us show the result in a message box in VBA.
Code:
Sub IIF_Example() Dim FinalResult As String Dim Number1 As Long Dim Number2 As Long Number1 = 105 Number2 = 100 FinalResult = IIf(Number1 > Number2, "Number 1 is Greater than Number 2", "Number 1 is Less than Number 2") MsgBox FinalResult End Sub
Now, let us run the code and see the result.
Since the Number 1 value is 105, which is greater than the Number 2 value of 100, we got the result as "Number 1 is Greater than Number 2". Therefore, since the Expression is TRUE, the IIF condition returned this result.
Example #2 - IF vs. IIF
You must be wondering what the difference between IF and IIF is. Yes, there is a difference in coding. For example, look at the IF condition code.
Code:
Sub IIF_Example() Dim FinalResult As String Dim Number1 As Long Dim Number2 As Long Number1 = 105 Number2 = 100 If Number1 > Number2 Then MsgBox "Number 1 is Greater than Number 2" Else MsgBox "Number 1 is Less than Number 2" End If End Sub
Using IF first, we have applied a logical test.
If Number1 > Number2 Then
Then if the logical test is true, we have applied the result.
MsgBox "Number 1 is Greater than Number 2"
Then if the logical test is false, we have applied the different results.
MsgBox "Number 1 is Less than Number 2"
Both functions return the same result, but with IIF, we can only code in a single line where the IF statement requires multiple lines.
Example #3 - VBA Nested IIF Condition
Like how we use nested IF to test multiple conditions similarly, we can also use multiple IIF. Take a look at the below code.
Code:
Sub IIF_Example2() Dim FinalResult As String Dim Marks As Long Marks = 98 FinalResult = IIf(Marks > 90, "Dist", IIf(Marks > 80, "First", IIf(Marks > 70, "Second", IIf(Marks > 60, "Third", "Fail")))) MsgBox FinalResult End Sub
The above IIF condition tests five logical tests and returns the result accordingly.