Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
542 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Written by Andy Brown
In this tutorial
At the heart of programming is the ability to test a condition. You can do this in Excel with a simple IF function:
A simple IF function, testing whether Olly the Owl is underpaid or not
In Excel VBA you also write conditions using the word IF, but the syntax is more flexible and is - usually - split over different lines. This tutorial shows how to use the IF / ELSE / END IF syntax followed by the alternative SELECT CASE syntax.
For all but the simplest conditions the SELECT CASE format will give code which is easier to write, comment and understand.
Let's start, then, with the simple IF condition!
The IF statement tests whether something is true or not, and has various different forms according to how many alternatives there are.
All of the examples below show how to display different messages according to the day of the week, using the Weekday function to return a number from 1 (Sunday) through to 7 (Saturday).
If we want to test if today is Friday, you can use the simplest possible IF statement:
Sub TestDay()
'if it's Friday, display message
If Weekday(Date) = vbFriday Then MsgBox "Hooray - it's Friday!"
End Sub
This form only works when you are only doing one thing if the condition is true.
You might be better advised to ignore the syntax above (even though it does work for single statements) and use the more flexible structure below:
Sub TestDay()
'if it's Friday, display message
If Weekday(Date) = vbFriday Then
'although there's only one statement here, we could put others
MsgBox "Hooray - it's Friday!"
End If
'anything you put here will run whether the condition was true or not
End Sub
The advantage of the above syntax is that it's easier to read, and easier to add more statements within the condition.
You don't have to use the indentation shown, but if you do it will make your code much easier to read!
If you want to test two possible conditions, use an ELSE statement:
Sub TestDay()
'if it's Friday, display message
If Weekday(Date) = vbFriday Then
'VBA will run these statements if the condition is true
MsgBox "Hooray - it's Friday!"
Else
'VBA will run these statements if the condition is false
MsgBox "Sorry - still not Friday"
End If
End Sub
You can test as many conditons as you like by adding ELSEIF clauses - here's one for the various parts of the week:
Sub TestDay()
'if it's Friday, display message
If Weekday(Date) = vbFriday Then
'it's the end of the week
MsgBox "Hooray - it's Friday!"
ElseIf Weekday(Date) = vbSunday Or _
Weekday(Date) = vbSaturday Then
'it's the weekend
MsgBox "Yeah! It's the weekend"
ElseIf Weekday(Date) = vbMonday Then
'it's Monday ...
MsgBox "Sorry - it's Monday"
Else
'it must be mid-week
MsgBox "Not long till Friday"
End If
End Sub
Note that you don't have to have an ELSE clause (although it's good programming practice to do so).
The conditions above can get quite hard to read. Rather than using IF statements, it's often clearer to use SELECT CASE instead (called SWITCH in some other languages). This tests the value of a variable or expression, then reacts differently for different values of it. For example:
Sub TestDay()
'display different messages for different days of the week
Select Case Weekday(Date)
Case vbFriday
'it's the end of the week
MsgBox "Hooray - it's Friday!"
Case vbSunday, vbSaturday
'it's the weekend
MsgBox "Yeah! It's the weekend"
Case vbMonday
'it's Monday ...
MsgBox "Sorry - it's Monday"
Case Else
'it must be mid-week
MsgBox "Not long till Friday"
End Select
End Sub
In the above macro, we evaluate the day of the week using Weekday(Date), then test this against various possible day numbers.
You don't need a Case Else statement, although it's good practice always to include one. Often it's a good idea to report an error in the Case Else clause, since if your code reaches it you must have failed to include all possible eventualities.
You can learn more about this topic on the following Wise Owl courses:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.