×

Excel VBA: IF…..THEN …ELSE Statement

VBA : IF…..THEN …ELSE Statement: This function enables you to check one condition and, based on that, then run one of the two statement blocks present. If the ‘IF’ condition is TRUE, IF statement and condition will run, if it is FALSE, then the ‘ELSE’ block will come in the loop. This statement will always have a closing ‘END IF’ statement else, VBA will give an error.

Syntax

If <Condition1> Then
     <Statements1>
 ,
 Else
     (Statement2>
 ,
 End If 

Example 1: The below program checks if in ‘Sheet1’, India is present or not – if it is, then it will return ‘Delhi’ else ‘Master cell is not India’

Sub If_Then_Else_Example1()
     'will check whether the data is India
     If Worksheets("Sheet1").Cells(2, 1).Value = "India" Then
         Worksheets("Sheet1").Cells(2, 2).Value = "Delhi"
         'If the above case is not true, it will come to Else condition.
     Else
         Worksheets("Sheet1").Cells(2, 2).Value = "Master cell is not India" 
     End If
 End Sub 

Output

IF…..THEN …ELSE Statement in VBA

Example 2: Write a macro, checking if the candidate has scored more that 61 percentile, he/she is passed else he/she is failed.   

Sub If_Then_Else_Example2()
     'Variable declaration
     Dim Marks As String
     'Accepting the value by the user
     Percentile = InputBox("Enter the candidate's percentile", "Marks")
     If Percentile >= 61 Then
         'Check if the Candidate's has been selected
         MsgBox "Candidate is been selected!"
     Else 
         'Check if the Candidate has failed
         MsgBox "Sorry! Better Luck next time. "
     End If
 End Sub 

Output

IF THEN  ELSE Statement in VBA2
IF THEN  ELSE Statement in VBA3

Example 3: Writing a macro, checking whether the user-entered number is an even or odd number.

Sub If_Then_Else_Example3()
     'Variable declaration
     Dim Number As Integer
     'Accepting a value by the user
     Number = InputBox("Enter a Number:", "Number")
     If Number Mod 2 = 0 Then
         'Checking if the number is even
         MsgBox "The given number is Even." 
     Else
         'Checking if the number is odd
         MsgBox "The given number is Odd."
     End If
 End Sub 

Output

IF THEN  ELSE Statement in VBA4
IF THEN  ELSE Statement in VBA5

Related Topics

VBA ActiveCell Property

What is the ActiveCell Property? The active cell signifies the active selected cell in the current worksheet. The Active property acts as a reference point and is used to move the cell cursor...

5 minutes read.

Userform Events

What are Events? Anything you do in to trigger an excel file is an event (an action). Example: If you want a greeting message ‘Good Day’ whenever an excel file is...

7 minutes read.

Excel VBA Sqr Function

VBA Sqr Function: The Sqr function in VBA returns the square root for the specified number. Syntax Sqr (Number) Parameter Number (required) – This parameter represents a positive numeric value that you want to calculate...

1 minute read.

Excel VBA IsNull Function

VBA IsNull Function: The IsNull function in VBA returns a Boolean, indicating whether a supplied expression is Null. Syntax IsNull (Expression) Parameter Expression (required)- This parameter represents the name of the argument that you want...

1 minute read.

VBA ListBox

What is a ListBox? ListBox refers to a permanently displayed control (usually box-shaped) which contains a list of objects (or attribute, or elements) from which the user can select single or multiple attributes....

8 minutes read.

Four VBA Clear methods

Four VBA Clear methods In Microsoft Excel, many times, a situation arises where the user wants to clear the data or any specific range of data. What if the user automates this task with...

6 minutes read.

Excel VBA CDate Function

VBA CDate Function: The CDate function in VBA converts an expression into a Date (or Time) data type. Syntax CDate (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to...

1 minute read.

VBA Charts

What is a Chart? A chart is used to visually show numbers or data in a spreadsheet (or spread over multiple spreadsheets) so that the end-user can look at the chart...

3 minutes read.

Excel VBA CByte Function

VBA CByte Function: The CByte function in VBA converts an expression into a Byte data type. Syntax CByte (Expression) Parameter Expression (required) - This parameter represents the expression that that you want to convert to...

1 minute read.

Excel VBA Oct Function

VBA Oct Function: The Oct function in VBA converts the given number into octal notation and returns the result as a string. Syntax Oct (Number) Parameter Number (required) – This parameter represents the numeric value...

1 minute read.

VBA Cell

What is VBA cell? Cells is one of the elements (workbook, worksheet, range) in Excel VBA, which refers to cells of the Excel worksheet.  In VBA, the cell is also a property...

5 minutes read.

Excel VBA Space Function

VBA Space Function: The Space function in VBA creates a String consisting of a specified number of spaces. Syntax Space (Number) Parameter Number (required) - This parameter represents the number of spaces. Return This function returns a...

1 minute read.

VBA Updating Pivot Table

VBA- Updating Pivot Table The pivot table is an important feature to explore, summarize, and interpret the bulk amount of data. It helps data evaluating, reviewing, as well as making useful...

3 minutes read.

Declaring a Variable in VBA

Declaring a Variable A variable is broadly described as a storage location combined with a name and representing a specific value. Declaring a variable is instructing the computer to reserve space...

2 minutes read.

UserForm and its Properties Excel VBA

UserForm and its Properties Userform has certain properties that can be viewed as category wise (based on appearance, behavior, font) or in an alphabetic manner. The property window is used to set or...

11 minutes read.

Excel VBA Left Function

VBA Left Function: The Left function in VBA returns a substring from the start of the specified string. Syntax Left (Str, Length) Parameter Str (required) – This parameter represents the string that you want to extract...

1 minute read.

Excel VBA CInt Function

VBA CInt Function: The VBA Cint function converts the specified expression into an Integer. Syntax Cint (Expression) Parameter Expression (required) – This parameter represents the expression that you want to convert to an Integer wherein...

1 minute read.

Excel VBA StrComp Function

VBA StrComp Function: The StrComp function in VBA compares two strings and returns an integer value displaying the result of the comparison. Syntax StrComp (String1, String2, [Compare]) Parameter String1 (required)- This parameter represents the first string to...

2 minutes read.

Excel VBA Exp Function

VBA Exp Function: The Exp function in VBA returns the value of the exponential function ex (mathematical constant ‘e’ raised to specified power) for the given value of x. Syntax Exp (Number) Parameter Number (required) –This parameter represents...

1 minute read.

Excel VBA: IF THEN Statement

VBA Excel: IF THEN StatementThis conditional statement enables you to check one condition and on the basis of that then run one or multiple statements if the condition holds. If...

1 minute read.