×

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

Excel VBA Mid Function

VBA Mid Function: The Mid function in VBA returns a substring from within a supplied string. Syntax Mid (Str, Start, [Length]) Parameter Str (required) -This parameter represents a string from which you want to extract the substring. Start...

1 minute read.

Excel VBA: Select … Case Statement

Select … Case Statement When a group of statements is executed, depending upon the value of an Expression, then Switch Case is used.  If you have several conditions to check, then the If condition...

4 minutes read.

VBA Type Mismatch Error

What is a Type Mismatch Error? VBA Type Mismatch Error is a run time error in excel, which often occurs when the data types contained in a VBA code are not matched...

3 minutes read.

Excel VBA Date Function

VBA Date Function The Date function in VBA returns the current date. Syntax Date( ) Parameter NA Return This function returns the current date. Example 1 Sub DateFunction_Example1() ' retuning the current date in the variable currentDate Dim currentDate As...

1 minute read.

Excel VBA LTrim Function

VBA LTrim Function: The LTrim function in VBA removes the leading spaces from a supplied text string. Syntax LTrim (String) Parameter String (required) - This parameter represents he text string that you want to remove...

1 minute read.

Excel VBA INT Function

VBA INT Function: The INT function in VBA rounds the given supplied number down and returns an integer value. The positive numbers are rounded to zero, and the negative numbers are rounded away from zero. Syntax Int...

1 minute read.

ActiveX Controls

ActiveX Controls are one of the most used Excel Controls to automate applications with Excel VBA. It has the same controls, unlike Form Controls (Command Button, combo box, checkbox, etc.), but it...

3 minutes read.

Excel VBA Join Function

Excel VBA Join Function: The Join function in VBA is used to join an array of substrings and return them all as a single string. Syntax Join (SourceArray, [Delimiter]) Parameter SourceArray (required) – This parameter...

1 minute read.

Excel VBA While wend Loop

WHILE wend loop is used when the user is not sure how many times they want to execute the VBA code within the program. With a WHILE loop, the loop body may...

3 minutes read.

VBA Global Variable

What is Global Variable? The Global Variables in VBA refers to the variables declared before the start of any macro. They are defined outside the functions and are used by all the functions or...

6 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 InputBox

Input Box The InputBox function in VBA is used to prompt the users to enter values. The user can click either the OK button or can choose the CANCEL button. If the user clicks...

3 minutes read.

Excel VBA Now Function

Excel VBA Now Function: The Now function in VBA returns the current date and time.  Syntax Now () Parameter NA Return This function returns the current date and time.  Example 1 Sub NowFunction_Example1() 'It will return...

1 minute read.

Excel VBA DateDiff Function

The DateDiff function in VBA returns a Long data value representing the number of intervals between two specified dates/times where the type of interval is supplied by the user. Syntax DateDiff (Interval, Date1, Date2, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required)...

2 minutes read.

Excel VBA FormatCurrency Function

VBA FormatCurrency Function: The FormatCurrency function in VBA is used to apply a currency format to a numeric expression and returns the result as a string. Syntax FormatCurrency (Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits]) Parameter Expression (required)...

2 minutes read.

Excel VBA TimeValue Function

Excel VBA TimeValue Function: The TimeValue function in VBA returns a Time from the specified String interpretation of a time /date where the date information for the given string is...

1 minute read.

Excel VBA CVErr Function

VBA CVErr Function: The CVErr function in VBA returns an Error data type, involving with a user-specified error code. Syntax CVErr (Expression) Parameter Expression (required)- This parameter represents the required error code. Return This function returns an...

1 minute read.

Excel Objects in VBA

What are Excel Objects? The Excel objects belong to the entities that make up an Excel Workbook, Worksheets, Columns, Rows, Cell Ranges, etc. Each object in Excel has loads of Properties that are...

6 minutes read.

Excel VBA InStr Function

VBA InStr Function The InStr function in VBA searches for a substring inside the given string. It returns the position of a substring within a string, as an integer if the substring is found...

1 minute read.

Excel VBA Choose Function

VBA Choose Function: The Choose function in VBA chooses function selects the corresponding value from a list of arguments depending as per the specified index. Syntax Choose (Index, [Choice-1], [Choice-2], ...) Parameter Index (required) – This...

2 minutes read.