MS Excel: How To Use The IF-THEN-ELSE Statement (VBA)
Có thể bạn quan tâm
- Home
- MS Excel
- Formulas / Functions
MS Excel: How to use the IF-THEN-ELSE Statement (VBA) This Excel tutorial explains how to use the Excel IF-THEN-ELSE statement (in VBA) with syntax and examples.
Description
The Microsoft Excel IF-THEN-ELSE statement can only be used in VBA code. It executes one set of code if a specified condition evaluates to TRUE, or another set of code if it evaluates to FALSE.
The IF-THEN-ELSE statement is a built-in function in Excel that is categorized as a Logical Function. It can be used as a VBA function (VBA) in Excel. As a VBA function, you can use this function in macro code that is entered through the Microsoft Visual Basic Editor.
Please read our IF function (WS) page if you are looking for the worksheet version of the IF statement as it has a very different syntax.
Subscribe
If you want to follow along with this tutorial, download the example spreadsheet.
Download Example
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 IfParameters 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. result_else The code that is executed when all previous conditions (condition1, condition2, ... condition_n) are false.Returns
The IF-THEN-ELSE statement evaluates the conditions in the order listed. It will execute the corresponding code when a condition is found to be true. If no condition is met, then the Else portion of the IF-THEN-ELSE statement will be executed.
Note
- The ElseIf and Else clauses are optional.
Applies To
- Excel for Office 365, Excel 2019, Excel 2016, Excel 2013, Excel 2011 for Mac, Excel 2010, Excel 2007, Excel 2003, Excel XP, Excel 2000
Type of Function
- VBA function (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 IfNext, 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 IfFinally, 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 IfExample#1 from Video
In the first video example, we are going to use the IF-THEN-ELSE statement to update cell C2 with "North", "South", "East" or "West" depending on the region code entered in cell A2.
So if we entered "N" in cell A2, we want "North" to appear in cell C2. If we entered "S" in cell A2, we want "South" to appear in cell C2, and so on.
Sub totn_if_example1() Dim LRegion As String Dim LRegionName As String LRegion = Range("A2").Value If LRegion = "N" Then LRegionName = "North" ElseIf LRegion = "S" Then LRegionName = "South" ElseIf LRegion = "E" Then LRegionName = "East" Else LRegionName = "West" End If Range("C2").Value = LRegionName End SubExample#2 from Video
In the second video example, we have a list of students in column A and their corresponding grade in column B. We want to update the comment value in column C based on the grade in column B.
So a grade of "A" or "B" will have a corresponding comment value of "Great Work", a grade of "C" will have a comment of "Needs Improvement", and all other grades will have the comment "Time for a Tutor".
Sub totn_if_example2() For Each grade In Range("B2:B8") If grade = "A" Or grade = "B" Then grade.Offset(0, 1).Value = "Great work" ElseIf grade = "C" Then grade.Offset(0, 1).Value = "Needs Improvement" Else grade.Offset(0, 1).Value = "Time for a Tutor" End If Next grade End Sub Share on:Databases
- SQL
- Oracle / PLSQL
- SQL Server
- MySQL
- MariaDB
- PostgreSQL
- SQLite
MS Office
- Excel
- Access
- Word
Web Development
- HTML
- CSS
- JavaScript
- Color Picker
Programming
- C Language
More
- ASCII
- Unicode
- Linux
- UNIX
- Techie Humor

Lookup/Ref Functions
- ADDRESS (WS)
- AREAS (WS)
- CHOOSE (WS, VBA)
- COLUMN (WS)
- COLUMNS (WS)
- HLOOKUP (WS)
- HYPERLINK (WS)
- INDEX (WS)
- INDIRECT (WS)
- LOOKUP (WS)
- MATCH (WS)
- OFFSET (WS)
- ROW (WS)
- ROWS (WS)
- TRANSPOSE (WS)
- VLOOKUP (WS)
- XLOOKUP (WS)

String/Text Functions
- ASC (VBA)
- BAHTTEXT (WS)
- CHAR (WS)
- CHR (VBA)
- CLEAN (WS)
- CODE (WS)
- CONCAT (WS)
- CONCATENATE (WS)
- CONCATENATE with & (WS, VBA)
- DOLLAR (WS)
- EXACT (WS)
- FIND (WS)
- FIXED (WS)
- FORMAT STRINGS (VBA)
- INSTR (VBA)
- INSTRREV (VBA)
- LCASE (VBA)
- LEFT (WS, VBA)
- LEN (WS, VBA)
- LOWER (WS)
- LTRIM (VBA)
- MID (WS, VBA)
- NUMBERVALUE (WS)
- PROPER (WS)
- REPLACE (WS)
- REPLACE (VBA)
- REPT (WS)
- RIGHT (WS, VBA)
- RTRIM (VBA)
- SEARCH (WS)
- SPACE (VBA)
- SPLIT (VBA)
- STR (VBA)
- STRCOMP (VBA)
- STRCONV (VBA)
- STRREVERSE (VBA)
- SUBSTITUTE (WS)
- T (WS)
- TEXT (WS)
- TEXTJOIN (WS)
- TRIM (WS, VBA)
- UCASE (VBA)
- UNICHAR (WS)
- UNICODE (WS)
- UPPER (WS)
- VAL (VBA)
- VALUE (WS)

Date/Time Functions
- DATE (WS)
- DATE (VBA)
- DATEADD (VBA)
- DATEDIF (WS)
- DATEDIFF (VBA)
- DATEPART (VBA)
- DATESERIAL (VBA)
- DATEVALUE (WS, VBA)
- DAY (WS, VBA)
- DAYS (WS)
- DAYS360 (WS)
- EDATE (WS)
- EOMONTH (WS)
- FORMAT DATES (VBA)
- HOUR (WS, VBA)
- ISOWEEKNUM (WS)
- MINUTE (WS, VBA)
- MONTH (WS, VBA)
- MONTHNAME (VBA)
- NETWORKDAYS (WS)
- NETWORKDAYS.INTL (WS)
- NOW (WS, VBA)
- SECOND (WS)
- TIME (WS)
- TIMESERIAL (VBA)
- TIMEVALUE (WS, VBA)
- TODAY (WS)
- WEEKDAY (WS, VBA)
- WEEKDAYNAME (VBA)
- WEEKNUM (WS)
- WORKDAY (WS)
- WORKDAY.INTL (WS)
- YEAR (WS, VBA)
- YEARFRAC (WS)

Math/Trig Functions
- ABS (WS, VBA)
- ACOS (WS)
- ACOSH (WS)
- AGGREGATE (WS)
- ASIN (WS)
- ASINH (WS)
- ATAN (WS)
- ATAN2 (WS)
- ATANH (WS)
- ATN (VBA)
- CEILING (WS)
- CEILING.PRECISE (WS)
- COMBIN (WS)
- COMBINA (WS)
- COS (WS, VBA)
- COSH (WS)
- DEGREES (WS)
- EVEN (WS)
- EXP (WS, VBA)
- FACT (WS)
- FIX (VBA)
- FLOOR (WS)
- FORMAT NUMBERS (VBA)
- INT (WS, VBA)
- LN (WS)
- LOG (WS)
- LOG (VBA)
- LOG10 (WS)
- MDETERM (WS)
- MINVERSE (WS)
- MMULT (WS)
- MOD (WS)
- MOD (VBA)
- ODD (WS)
- PI (WS)
- POWER (WS)
- PRODUCT (WS)
- RADIANS (WS)
- RAND (WS)
- RANDBETWEEN (WS)
- RANDOMIZE (VBA)
- RND (VBA)
- ROMAN (WS)
- ROUND (WS)
- ROUND (VBA)
- ROUNDDOWN (WS)
- ROUNDUP (WS)
- SGN (VBA)
- SIGN (WS)
- SIN (WS, VBA)
- SINH (WS)
- SQR (VBA)
- SQRT (WS)
- SUBTOTAL (WS)
- SUM (WS)
- SUMIF (WS)
- SUMIFS (WS)
- SUMPRODUCT (WS)
- SUMSQ (WS)
- SUMX2MY2 (WS)
- SUMX2PY2 (WS)
- SUMXMY2 (WS)
- TAN (WS, VBA)
- TANH (WS)
- TRUNC (WS)

Statistical Functions
- AVEDEV (WS)
- AVERAGE (WS)
- AVERAGEA (WS)
- AVERAGEIF (WS)
- AVERAGEIFS (WS)
- BETA.DIST (WS)
- BETA.INV (WS)
- BETADIST (WS)
- BETAINV (WS)
- BINOM.DIST (WS)
- BINOM.INV (WS)
- BINOMDIST (WS)
- CHIDIST (WS)
- CHIINV (WS)
- CHITEST (WS)
- COUNT (WS)
- COUNTA (WS)
- COUNTBLANK (WS)
- COUNTIF (WS)
- COUNTIFS (WS)
- COVAR (WS)
- FORECAST (WS)
- FREQUENCY (WS)
- GROWTH (WS)
- INTERCEPT (WS)
- LARGE (WS)
- LINEST (WS)
- MAX (WS)
- MAXA (WS)
- MAXIFS (WS)
- MEDIAN (WS)
- MIN (WS)
- MINA (WS)
- MINIFS (WS)
- MODE (WS)
- MODE.MULT (WS)
- MODE.SNGL (WS)
- PERCENTILE (WS)
- PERCENTRANK (WS)
- PERMUT (WS)
- QUARTILE (WS)
- RANK (WS)
- SLOPE (WS)
- SMALL (WS)
- STDEV (WS)
- STDEVA (WS)
- STDEVP (WS)
- STDEVPA (WS)
- VAR (WS)
- VARA (WS)
- VARP (WS)
- VARPA (WS)

Logical Functions
- AND (WS)
- AND (VBA)
- CASE (VBA)
- FALSE (WS)
- FOR...NEXT (VBA)
- IF (WS)
- IF (more than 7) (WS)
- IF (up to 7) (WS)
- IF-THEN-ELSE (VBA)
- IFERROR (WS)
- IFNA (WS)
- IFS (WS)
- NOT (WS)
- OR (WS)
- OR (VBA)
- SWITCH (WS)
- SWITCH (VBA)
- TRUE (WS)
- WHILE...WEND (VBA)

Information Functions
- CELL (WS)
- ENVIRON (VBA)
- ERROR.TYPE (WS)
- INFO (WS)
- ISBLANK (WS)
- ISDATE (VBA)
- ISEMPTY (VBA)
- ISERR (WS)
- ISERROR (WS, VBA)
- ISLOGICAL (WS)
- ISNA (WS)
- ISNONTEXT (WS)
- ISNULL (VBA)
- ISNUMBER (WS)
- ISNUMERIC (VBA)
- ISREF (WS)
- ISTEXT (WS)
- N (WS)
- NA (WS)
- TYPE (WS)

Financial Functions
- ACCRINT (WS)
- ACCRINTM (WS)
- AMORDEGRC (WS)
- AMORLINC (WS)
- DB (WS)
- DDB (WS, VBA)
- FV (WS, VBA)
- IPMT (WS, VBA)
- IRR (WS, VBA)
- ISPMT (WS)
- MIRR (WS, VBA)
- NPER (WS, VBA)
- NPV (WS, VBA)
- PMT (WS, VBA)
- PPMT (WS, VBA)
- PV (WS, VBA)
- RATE (WS, VBA)
- SLN (WS, VBA)
- SYD (WS, VBA)
- VDB (WS)
- XIRR (WS)

Database Functions
- DAVERAGE (WS)
- DCOUNT (WS)
- DCOUNTA (WS)
- DGET (WS)
- DMAX (WS)
- DMIN (WS)
- DPRODUCT (WS)
- DSTDEV (WS)
- DSTDEVP (WS)
- DSUM (WS)
- DVAR (WS)
- DVARP (WS)

Engineering Functions
- BIN2DEC (WS)
- BIN2HEX (WS)
- BIN2OCT (WS)
- COMPLEX (WS)
- CONVERT (WS)

File/Directory Functions
- CHDIR (VBA)
- CHDRIVE (VBA)
- CURDIR (VBA)
- DIR (VBA)
- FILEDATETIME (VBA)
- FILELEN (VBA)
- GETATTR (VBA)
- MKDIR (VBA)
- SETATTR (VBA)

Data Type Conv. Functions
- CBOOL (VBA)
- CBYTE (VBA)
- CCUR (VBA)
- CDATE (VBA)
- CDBL (VBA)
- CDEC (VBA)
- CINT (VBA)
- CLNG (VBA)
- CSNG (VBA)
- CSTR (VBA)
- CVAR (VBA)
Home | About Us | Contact Us | Testimonials | Donate
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.
Copyright © 2003-2025 TechOnTheNet.com. All rights reserved.
Từ khóa » Visual Basic If
-
If...Then...Else Statement - Visual Basic - Microsoft Docs
-
#If...Then...#Else Directives - Visual Basic | Microsoft Docs
-
Lệnh If Else Trong Visual Basic: Dùng để Rẻ Nhánh Chương Trình
-
VB.Net - If...Then...Else Statement - Tutorialspoint
-
Visual Basic If Statement - Tutlane
-
Visual Basic If-Else-If Statement - Tutlane
-
Hướng Dẫn Cách Viết Cấu Trúc IF THEN ELSE Trong VBA Excel
-
HƯỚNG DẪN ĐẦY ĐỦ CÂU LỆNH IF TRONG VBA - Học Excel Online
-
Visual Basic If Statement - The Coding Guys
-
Visual Basic 6 Tutorial => If / Else Statement
-
Mệnh đề If-else Trong VBA - VietTuts
-
VB.NET If Then, ElseIf, Else Examples
-
If Then Else Statement - VB.NET
-
How To Use The IF Keyword In Visual Basic - Unaura