Conditions in Visual Basic for Applications - IF and SELECT CASE
Part one of a three-part series of blogs

This part of the Excel VBA training series of blogs shows how to use IF / ELSE / END IF within VBA, and also how to use the alternative SELECT / CASE syntax within macros.

  1. Using Conditions in VBA - IF and SELECT / CASE (this blog)
  2. Writing IF Conditions in Visual Basic for Applications
  3. Using SELECT CASE to Test Conditions in VBA macros

This series of blogs is part of our Excel VBA online tutorial.  Alternatively, why not have a look at our Excel courses or VBA training?

Posted by Andy Brown on 15 November 2011

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Using Conditions in VBA - IF and SELECT / CASE

At the heart of programming is the ability to test a condition.  You can do this in Excel with a simple IF function:

Example of 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 blog 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!


This blog has 0 threads Add post