×

Excel VBA Conditional Statement

Conditional Statement in VBA Excel

Conditional Statements in Excel VBA are one of the most powerful and useful features in programming, this will give you to perform comparisons to decide or loop through a specified number of iterations based on certain criteria.

The various conditional statements are as follows:

  1. IF….THEN statement
  2. IF…..THEN …ELSE Statement
  3. If … ElseIf … Else Statement
  4. If … ElseIf … ElseIf Statement or Nested If statement
  5. Select … Case

Example 1

Sub Conditional_Statement_Example1()
Dim CurRow As Byte
CurRow = 2
'Using Do Until Loop
Do Until Cells(CurRow, 1) = ""
'If..Else statement
If Cells(CurRow, 3) > 200 Then
Cells(CurRow, 5) = "Qualified" 
Else
Cells(CurRow, 5) = "Not Qualified"
'Closing the IF
End If
CurRow = CurRow + 1
Loop
End Sub 

Output

Conditional Statements in Excel VBA

Example 2

Sub Conditional_Statement_Example2() 
  'Variable declaration
  Dim Marks As String
  'Accepting the month by the user
  Marks = InputBox("Enter the students percentile marks:", "Marks")
  If Marks <= 100 And Marks >= 91 Then
  'Check if the Student's Grade is A1 
  MsgBox "Grade : A1"
  ElseIf Marks < 91 And Marks >= 81 Then
  'Check if the Student's Grade is A2
  MsgBox "Grade : A2"
  ElseIf Marks < 81 And Marks >= 71 Then
  'Check if the Student's Grade is B1
  MsgBox "Grade : B1" 
  ElseIf Marks < 71 And Marks >= 61 Then
  'Check if the Student's Grade is B2
  MsgBox "Grade : B2"
  ElseIf Marks < 61 And Marks >= 51 Then
  'Check if the Student's Grade is C1
  MsgBox "Grade : C1"
  ElseIf Marks < 51 And Marks >= 41 Then
  'Check if the Student's Grade is C2
  MsgBox "Grade : C2" 
  ElseIf Marks < 40 And Marks >= 33 Then
  'Check if the Student's Grade is D
  MsgBox "Grade : D"
  Else
  'Check if the student has failed
  MsgBox "Grade : Fail" 
  End If
 End Sub 

Output

VBA Conditional Statement
Conditional Statement

Example 3

Sub Conditional_Statement_Example3()
Select Case Range("B2")
Case "C2"
Range("C2") = Range("B2") / 100
Case "D2"
Range("D2") = Range("B2") / 100
Case "D3", "D4"
Range("E2") = Range("B2") / 100
Case Else 
Range("F2") = Range("B2") / 100
End Select
End Sub 

Output

Sub Conditional Statement Example3

Related Topics

Excel VBA ABS Function

VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number. Syntax Abs (Number) Parameter Number (required) – This parameter represents the number that you want the absolute value of. Return This...

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.

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 Time Function

Excel VBA Time Function: The Time function in VBA returns the current time. Syntax Time () Parameter NA Return This function returns the current time.  Example 1 Sub TimeFunction_Example1() ' returns the current time Dim time_val...

1 minute read.

VBA ActiveSheet

What is VBA Activesheet Property? The active sheet means the current worksheet which you are working on and viewing. The ActiveSheet object signifies the worksheet tab that is selected before running the VBA...

5 minutes 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 Pivot Table

What is a Pivot Table? One of the most powerful features of Excel VBA is the Pivot Table. A pivot table is a VBA tool that is used to create summary...

3 minutes read.

Excel VBA IsDate Function

VBA IsDate Function: The IsDate function in VBA returns a Boolean value indicating whether the given expression is interpreted as a VBA Date or not. Syntax IsDate (Expression) Parameter Expression (required) – This parameter...

1 minute read.

Excel VBA Filter Function

VBA Filter Function: The Filter function in VBA returns a subset for the given string array, based on specified criteria. Syntax Filter (SourceArray, Match, [Include], [Compare]) Parameter SourceArray (required) – This parameter the array of Strings that you...

2 minutes read.

Excel VBA CDbl Function

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

1 minute read.

Scope in Visual Basics

Definition of Scope The scope of any programming language implies the area of code where the variables will be identified, accessed, and used. Every variable has a scope associated with it. The scope of...

4 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 MessageBox

Message Box The MsgBox in Excel VBA is a dialog box used to inform the users of your program by showing a custom message or get some necessary inputs such as Yes/No or...

4 minutes read.

Excel VBA IsNumeric Function

VBA IsNumeric Function: The IsNumeric function in VBA returns a Boolean value showing whether the specified Expression contains a numeric value or not. Syntax IsNumeric (Expression) Parameter Expression (required)- This parameter represents the variant that...

1 minute read.

VBA Regex

What is a Regex? Regex stands for Regular Expression is basically a pattern matching strings within another string. They are supported in many languages, including .net, C++, Python, etc. They are...

5 minutes read.

Excel VBA CSng Function

VBA CSng Function: The CSng function in VBA converts the supplied expression into a single-precision floating-point number (single data type). Syntax CSng (Expression) Parameter Expression (required) – This parameter represents the expression that you want...

1 minute read.

Excel VBA DateAdd Function

The DateAdd function in VBA adds a time interval to a supplied date and/or time and returns the resultant date/time. Syntax Dateadd (Interval, Number, Date) Parameter Interval (required) – This parameter a string specifying the interval to be...

1 minute read.

Excel VBA IsObject Function

VBA IsObject Function: The IsObject function in VBA returns a Boolean value showing whether the specified variable represents an Object variable type or not. Syntax IsObject (Expression) Parameter Expression (required)- This parameter represents the...

1 minute read.

Excel VBA Tan Function

VBA Tan Function: The Tan function in VBA returns the tangent for the specified angle in radians. Syntax Tan (Number) Parameter Number (required) – This parameter represents the angle supplied in radiant that you want...

1 minute read.

Excel VBA Len Function

VBA Len Function: The Len function in VBA returns the number of characters in a supplied string or the number of bytes required to store a supplied variable. Syntax Len (Expression) Parameter Expression (required)-...

1 minute read.