Professional Documents
Culture Documents
If Condition Macro Mastery
If Condition Macro Mastery
Contents [hide]
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 2 of 41
9 Using If And/If Or
9.1 Using If And
9.2 Using If Or
9.3 Using If Not
10 The IIF function
10.1 Using Nested IIf
10.2 What to Watch Out For
10.3 If Versus IIf
11 Using Select Case
11.1 Using Case Is
12 Try this Exercise
12.1 Answer to Exercise
13 What’s Next?
box/145db6b73f72a2%
14 Get the Free eBook
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 3 of 41
If without Endif If [condition is true] Then [do If value <= 0 Then value
(One line only) something] =0
The following code shows a simple example of using the VBA If statement
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 4 of 41
You will often want to make choices based on the data your macros reads.
For example, you may want to read only the students who have marks greater than
70. As you read through each student you would use the If Statement to check the
marks of each student.
The important word in the last sentence is check. The If statement is used to check
a value and then to perform a task based on the results of that check.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 5 of 41
(https://excelmacromastery.leadpages.co/leadbox/145d7f073f72a2%
3A106f25298346dc/5630978613575680/)
Every time you use an If Then statement you must use a matching End If
statement.
When the condition evaluates to true, all the lines between If Then and End If are
We processed.
use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 6 of 41
To make your code more readable it is good practice to indent the lines between
the If Then and End If statements.
To indent the code you can highlight the lines to indent and press the Tab key.
Pressing Shift + Tab will Outdent the code i.e. move it one tab to the left.
You can also use the icons from the Visual Basic Toolbar to indent/outdent the
code
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 7 of 41
If you look at any code examples on this website you will see that the code is
indented.
Sub ReadMarks()
Dim i As Long
' Go through the marks columns
For i = 2 To 11
' Check if marks greater than 50
If Range("C" & i) > 50 Then
' Print student name to the Immediate Window(Ctrl + G)
Debug.Print Range("A" & i) & " " & Range("B" & i)
End If
Next
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
End Sub
Read more (https://excelmacromastery.com/cookie-policy/)
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 8 of 41
Results
Bryan Snyder
Juanita Moody
Douglas Blair
Leah Frank
Monica Banks
Play around with this example and check the value or the > sign and see how the
results change.
If Conditions
The piece of code between the If and the Then keywords is called the condition. A
condition is a statement that evaluates to true or false. They are mostly used with
Loops and If statements. When you create a condition you use signs like
>,<,<>,>=,<=,=.
x=5 x is equal to 5
x <>
We use 5
cookies to ensure that we give youxthe
does
bestnot equal on
experience 5 our website. If you continue to use this site
we will assume that you are happy with it. Ok
x > 5 And x < 10 Read more (https://excelmacromastery.com/cookie-policy/)
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 9 of 41
You may have noticed x=5 as a condition. This should not be confused with x=5
when used as an assignment.
When equals is used in a condition it means “is the left side equal to the right side”.
The following table demonstrates how the equals sign is used in conditions and
assignments
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 10 of 41
The last entry in the above table shows a statement with two equals. The first
equals sign is the assignment and any following equals signs are conditions.
This might seem confusing at first but think of it like this. Any statement that starts
with a variable and an equals is in the following format
So whatever is on the right of the equals sign is evaluated and the result is placed
in the variable. Taking the last three assignments again, you could look at them like
this
Using If ElseIf
The ElseIf statement allows you to choose from more than one option. In the
following example we print for marks that are in the Distinction or High Distinction
range.
Sub UseElseIf()
End
We use Sub to ensure that we give you the best experience on our website. If you continue to use this site
cookies
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 11 of 41
Let’s swap around the If and ElseIf from the last example. The code now look like
this
Sub UseElseIfWrong()
End Sub
In this case we check for a value being over 75 first. We will never print “High
Distinction” because if a value is over 85 is will trigger the first if statement.
To avoid these kind of problems we should use two conditions. These help state
exactly what you are looking for a remove any confusion. The example below
shows how to use these. We will look at more multiple conditions in the section
below.
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 12 of 41
Let’s expand the original code. You can use as many ElseIf statements as you like.
We will add some more to take into account all our mark classifications.
Using If Else
The Else statement is used as a catch all. It basically means “if no conditions were
true” or “everything else”. In the previous code example, we didn’t include a print
statement for a fail mark. We can add this using Else.
Sub UseElse()
End Sub
Let’s write some code to through our sample data and print the student and their
We classification.
use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 13 of 41
Sub AddClass()
End Sub
The results look like this with column E containing the classification of the marks
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 14 of 41
(https://excelmacromastery.com/wp-
content/uploads/2015/10/VBA-If-ElseIf-Class.png)
Results
Using If And/If Or
You can have more than one condition in an If Statement. The VBA keywords And
and Or allow use of multiple conditions.
These words work in a similar way to how you would use them in English.
Let’s look at our sample data again. We now want to print all the students that got
over between 50 and 80 marks.
We use And to add an extra condition. The code is saying: if the mark is greater
than or equal 50 and less than 75 then print the student name.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 15 of 41
Sub CheckMarkRange()
Next
End Sub
Results
Douglas Blair
Leah Frank
Monica Banks
In our next example we want the students who did History or French. So in this
case we are saying if the student did History OR if the student did French.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 16 of 41
Sub ReadMarksSubject()
Next
End Sub
Results
Bryan Snyder
Bradford Patrick
Douglas Blair
Ken Oliver
Leah Frank
Rosalie Norman
Jackie Morgan
Using Multiple conditions like this is often a source of errors. The rule of thumb to
remember is to keep them as simple as possible.
Using If And
We The AND works
use cookies as that
to ensure follows
we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 17 of 41
What you will notice is that AND is only true when all conditions are true
Using If Or
The OR keyword works as follows
What you will notice is that OR is only false when all the conditions are false.
Mixing AND and OR together can make the code difficult to read and lead to errors.
Using parenthesis can make the conditions clearer.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 18 of 41
Sub OrWithAnd()
End Sub
Using If Not
There is also a NOT operator. This returns the opposite result of the condition.
Condition Result
TRUE FALSE
FALSE TRUE
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
as are we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 19 of 41
If True Then
If Not False Then
and
If False Then
If Not True Then
A common usage of Not when checking if an object has been set. Take a
worksheet for example. Here we declare the worksheet
We want to check mySheet is valid before we use it. We can check if it is nothing.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 20 of 41
If you find this a bit confusing you can use parenthesis like this
=IF(F2=””,””,F1/F2)
The format is
VBA has the IIf statement which works the same way. Let’s look at an example. In
the following code we use IIf to check the value of the variable val. If the value is
greater than 10 we print true otherwise we print false.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 21 of 41
Sub CheckVal()
End Sub
In our next example we want to print out Pass or Fail beside each student
depending on their marks. In the first piece of code we will use the normal VBA If
statement to do this.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 22 of 41
Sub CheckMarkRange()
Next
End Sub
In the next piece of code we will use the IIf function. You can see that the code is
much neater here.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 23 of 41
Sub CheckMarkRange()
Next
End Sub
You can see the IIf function is very useful for simple cases where you are dealing
with two possible options.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 24 of 41
Sub CheckResultType2()
Next
End Sub
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 25 of 41
Sub UsingNestedIIF()
Next
End Sub
Using nested IIf is fine in simple cases like this. The code is simple to read and
therefore not likely to have errors.
In the following example we want to divide by marks when it does not equal zero. If
it equals zero we want to return zero.
marks = 0
total = IIf(marks = 0, 0, 60 / marks)
We However,
use cookieswhen
to ensure that we
marks give you
is zero thethe best will
code experience
give aon
“ our website. If you continue
” error. to use
This is this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 26 of 41
marks = 0
If marks = 0 Then
'Only executes this line when marks is zero
total = 0
Else
'Only executes this line when marks is Not zero
total = 60 / marks
End If
What this also means is that if you have Functions for True and False then both will
be executed. So IIF will run both Functions even though it only uses one return
value. For example
If Versus IIf
So which is better?
You can see for this case that IIf is shorter to write and neater. However if the
conditions get complicated you are better off using the normal If statement. A
disadvantage of IIf is that it is not well known so other users may not understand it
as well as code written with a normal if statement.
Also as we discussed in the last section IIF always evaluates the True and False
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 27 of 41
function calls. For more complex cases use the normal If statement.
The format is
Let’s take our AddClass example from above and rewrite it using a Select Case
statement.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 28 of 41
Sub AddClass()
End Sub
The following is the same code using a Select Case statement. The main thing you
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
<=100”.
Read more (https://excelmacromastery.com/cookie-policy/)
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 29 of 41
Sub AddClassWithSelect()
End Sub
Using Casewe Is
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 30 of 41
You can use Is to check for multiple values. In the following code we are checking
if marks equals 5, 7 or 9.
Sub TestMultiValues()
End Sub
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 31 of 41
We covered a lot in this post about the If statement. A good way to help you
understand it is by trying to write some code using the topics we covered. The
following exercise uses the test data from this post. The answer to the exercise is
below.
If cell G1 contains “French” then your result should look like this
(https://excelmacromastery.com/wp-
content/uploads/2015/11/exerciseResult.png)
Result of exercise
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 32 of 41
The following code shows how to complete the above exercise. Note: There are
many ways to complete this so don’t be put off if your code is different.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 33 of 41
Sub WriteSubjectResults()
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 34 of 41
outRow = outRow + 1
End If
Next i
End Sub
What’s Next?
Free VBA Tutorial If you are new to VBA or you want to sharpen your existing VBA
skills then why not try out the The Ultimate VBA Tutorial
(https://excelmacromastery.com/vba-tutorial-1/).
Related Training: Get full access to the Excel VBA training webinars and all the
tutorials (https://excelmacromastery.com/access_vault/).
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 35 of 41
(https://excelmacromastery.lpages.co/leadbox/14791da73f72a2%
3A106f25298346dc/5636318331666432/)
Please feel free to subscribe to my newsletter and get exclusive VBA content that
you cannot find here on the blog, as well as free access to my eBook, How to Ace
the 21 Most Common Questions in VBA which is full of examples you can use in
your own code.
(https://excelmacromastery.lpages.co/leadbox/14791da73f72a2%
3A106f25298346dc/5636318331666432/)
(https://excelmacromastery.lpages.co/leadbox/14791da73f72a2%
3A106f25298346dc/5636318331666432/)
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 36 of 41
Previous
Excel VBA Find – A Complete Guide
(https://excelmacromastery.com/excel-vba-find/)
Next
Excel VBA Dictionary – A Complete Guide
(https://excelmacromastery.com/vba-dictionary/)
12 COMMENTS
David O'Neill
May 18, 2016 at 10:04 am (https://excelmacromastery.com/vba-if/#comment-308)
You might want to include a warning about IIF’s nasty practice of fully evaluating
both the True and False part before selecting the appropriate one. This is bad
enough when you get an error (e.g. IIF(1=1,99/1,99/0) always results in a Divide by
Zero), but it can be a lot trickier when Functions are involved!
Reply
Paul Kelly
May 19, 2016 at 8:48 am (https://excelmacromastery.com/vba-if/#comment-309)
Thanks David.
Reply
Narendra
May 22, 2016 at 8:09 am (https://excelmacromastery.com/vba-if/#comment-310)
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 37 of 41
Narendra
Reply
Mary
June 16, 2016 at 7:49 pm (https://excelmacromastery.com/vba-if/#comment-311)
Hi, I have to write an IF statement when 4 out of 6 conditions are true (4 out of 6
months have a certain condition). Is there a way to combine it to say that If (for
example) 4 out of 6 months are = 1.3* a cell, then do this?
Your website is very helpful; thank you!
Reply
Paul Kelly
June 17, 2016 at 8:06 am (https://excelmacromastery.com/vba-if/#comment-
312)
Hi Mary if the six months are in a range of cells you could use
CountIf like this
If WorksheetFunction.CountIf(Range("A1:A6"), 1.3) = 4
Then
' Do something
End If
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 38 of 41
Sub CheckOccurences()
If cnt = 4 Then
' Do something
End If
End Sub
Reply
Debraj
September 18, 2017 at 7:25 pm (https://excelmacromastery.com/vba-if/#comment-16525)
Type Duration
Other 1:43:08
Break 0:52:44
BB_Bid_Lead 0:16:18
BB_Bid_Lead 0:20:16
BB_Bid_Lead 0:20:16
BB_Bid_Lead 0:12:11
BB_Bid_Lead 0:18:21
Other 1:24:26
BB_Bid_Lead 0:12:48
BB_Bid_Lead 0:10:30
Other 0:23:25
BB_Bid_Lead 0:18:16
We BB_Bid_Lead 0:11:36
use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 39 of 41
duration
Reply
Paul Kelly
September 19, 2017 at 8:48 am (https://excelmacromastery.com/vba-
if/#comment-16547)
Reply
Kantharaj
September 25, 2017 at 10:20 am (https://excelmacromastery.com/vba-if/#comment-16811)
Reply
Paul Kelly
September 25, 2017 at 8:39 pm (https://excelmacromastery.com/vba-
if/#comment-16825)
Do you mean
Reply
Harish S
September 26, 2017 at 3:42 am (https://excelmacromastery.com/vba-if/#comment-16841)
Thank you so much for your explanation. I was struck with doing an if loop macro
We for
use a
cookies to ensure
long time that job.
for my we give
Nowyouits
theabest experience on our website. If you continue to use this site
success.
we will assume that you are happy with it. Ok
Reply Read more (https://excelmacromastery.com/cookie-policy/)
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 40 of 41
Naresh kumar
February 20, 2018 at 3:02 am (https://excelmacromastery.com/vba-if/#comment-24234)
Hello Paul,
Reply
Naresh kumar
February 20, 2018 at 3:04 am (https://excelmacromastery.com/vba-if/#comment-
24235)
Reply
LEAVE A REPLY
Your email address will not be published. Required fields are marked *
Name *
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
Email *
Read more (https://excelmacromastery.com/cookie-policy/)
https://excelmacromastery.com/vba-if/ 5/22/2018
VBA IF Statement - A Complete Guide - Excel Macro Mastery Page 41 of 41
Website
Post Comment
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site
we will assume that you are happy with it. Ok
https://excelmacromastery.com/vba-if/ 5/22/2018