Using the Select Case Statement

Access Tips from Xlteq

Using the Select Case Statement

Often in VBA code you need to take a value and “do something” conditionally.

The traditional approach is to use an If Then Else or If Then Elseif ElseIf … type of construct, however these quickly get messy and hard to follow once you get above two or three choices. 

Take the not so well written example below which makes some different actions happen depending on the value selected in a combo box called MyCombo;

Whilst not complicated it is a bit messy and if you are trying to follow not so obvious what value is getting set. Let us do the same thing but with the often forgotten Select Case method.

This is much easier to read, and it is easier to follow the flow of the choices and results. Note that it accepts multiple number ranges as well (1,5-7).

Cleaner code and less typing as well. The only consideration to make is that the Select Case method carries out the conditions until one is met then exits, it will not carry on evaluating the other options. Normally this is what you want, but always bear it in mind.

Contact us for a free consultation

Either call us now on 020 3817 6945 or fill out the form below and one of our team will be in touch.

The contact form requires that you configure reCAPTCHA in the site configuration.

The contact form requires that you select an email template.