Excel VBA IF Else Statement
There is no IF Else statement in worksheet and it can be only used in the VBA code, while working in VBA we can provide a condition which is the If condition statement and if it is fulfilled a certain set of instructions then it is executed and if the condition fails the instruction then else statement is executed.
VBA is not different when it comes to logical tests, it works the same way like how it works in regular worksheets. Of all logical functions “IF” function is mostly used from the lot. Using IF we can conduct a logical test and arrive at the decisions if the logical test is satisfied and also arrive alternative decisions if the logical test is not satisfied.
Below is the syntax of the IF Else condition.
IF <Logical Test> Then If the Logical Test is TRUE Else If the Logical Test is FALSE End IF
What is VBA IF Then Else Statement?
Once the supplied logical test is FALSE we need some alternative task to execute as part of the code. So “IF ELSE” means if the logical test is FALSE then what else needs to do.
To better understand in the below example we have supplied the result as “10 is greater” only if the logical test is TRUE but in case of FALSE logical result we can supply the alternative result as “10 is lesser”.
So once the logical tests are supplied and TRUE part code written in the next line enter the word “ELSE”.
ELSE means if the logical test is not TRUE we need the result as “10 is lesser”.
Sub IF_Else_Example1() If 10 > 11 Then MsgBox "10 is greater" Else MsgBox "10 is lesser" End If End Sub
Now our code gives at least any one of the above results. Execute the code and see the result.
Since we have supplied the alternative result if the logical test is FALSE, it has displayed the alternative result as “10 is lesser” because 10 is less than the other number 11.
For an example look at the below data.
With this data, we need to arrive the status based on the “Cost” of each product. To arrive status below are the criteria’s.
If the cost price is > 50 the status should be “Expensive” or else status should be “Not Expensive”.
Here we need to test the cost price i.e. logical test whether cost price is >50 or not. If the logical test is TRUE i.e. cost price is more than 50 we need the status as “Expensive” and if the logical test is FALSE i.e. cost price is less than 50 we need the alternative result as “Not Expensive”.
Ok, let’s write the code now. Before that copy and paste the above table to excel worksheet.
Step 1: Start the subprocedure.
Sub IF_ELSE_Example2() End Sub
Step 2: Declare the variable as an Integer data type.
Dim k As Integer
Step 3: Since we need to test more than one cell values we need to employ FOR VBA LOOP to loop through the cells and apply the logic for all the cells.
Here we need to apply logical test from 2nd row to 8th row, so start FOR LOOP from 2 to 8.
For k = 2 To 8 Next k
Step 4: Inside this loop, we need to execute the logical test. So open IF statement and select the first cell by using CELLS property.
If Cells(k, 2).Value > 50 Then
Here Cells(k, 2) means row (value of k) and column 2.
Step 5: If this cell value is > 50 then we need the result as “Expensive” in the next column cell. So code will be –
Cells(k, 3).Value = "Expensive"
Step 6: If the test isn’t TRUE then we need ELSE statement result i.e. “Not Expensive”.
Sub IF_ELSE_Example2() Dim k As Integer For k = 2 To 8 If Cells(k, 2).Value > 50 Then Cells(k, 3).Value = "Expensive" Else Cells(k, 3).Value = "Not Expensive" End If Next k End Sub
Now, this code will loop through from 2nd to 8th-row tests the numbers and arrive at the result based on the cost price.
Like this, we can use If-Else to arrive alternative results.
Things to Remember
- Else statement is for a FALSE logical test.
- If you want to apply more than two logical tests in excel then we need to use the ELSE IF statement.
- In case of task execution for more than one cell we need to use loops.
- If Else statement can test only one logical test.
This has been a guide to VBA If Else. Here we discuss how to use If Then Else Statement along with a practical example and downloadable excel template. You may learn more about VBA from the following articles –