Description
The Microsoft Excel IF-THEN-ELSE statement can only be used in VBA code. It returns a value if a specified condition evaluates to TRUE, or another value if it evaluates to FALSE.
Syntax
The syntax for the IF-THEN-ELSE statement in Microsoft Excel is:
If condition_1 Then
result_1
ElseIf condition_2 Then
result_2
...
ElseIf condition_n Then
result_n
Else
result_else
End If
Parameters or Arguments
- condition_1, condition_2, ... condition_n
- The conditions that are to be evaluated in the order listed. Once a condition is found to be true, the corresponding code will be executed. No further conditions will be evaluated.
- result_1, result_2, ... result_n
- The code that is executed once a condition is found to be true.
Note
- If no condition is met, then the Else portion of the IF-THEN-ELSE statement will be executed.
- It is important to note that the ElseIf and Else portions are optional.
Applies To
- Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000
Type of Function
- VBA statement (VBA)
Example (as VBA Function)
The IF-THEN-ELSE statement can only be used in VBA code in Microsoft Excel.
Let's look at some Excel IF-THEN-ELSE statement function examples and explore how to use the IF-THEN-ELSE statement in Excel VBA code:
First, let's look at a simple example.
If LRegion ="N" Then
LRegionName = "North"
End If
Next, let's look at an example that uses ElseIf.
If LRegion ="N" Then
LRegionName = "North"
ElseIf LRegion = "S" Then
LRegionName = "South"
ElseIf LRegion = "E" Then
LRegionName = "East"
ElseIf LRegion = "W" Then
LRegionName = "West"
End If
Finally, let's look at an example that uses Else.
If LRegion ="N" Then
LRegionName = "North"
ElseIf LRegion = "S" Then
LRegionName = "South"
ElseIf LRegion = "E" Then
LRegionName = "East"
Else
LRegionName = "West"
End If
No comments:
Post a Comment