P. 1
Vba for Excel Made Simple

Vba for Excel Made Simple


|Views: 3,692|Likes:
Published by fmunir1

More info:

Published by: fmunir1 on Aug 18, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





Consider the If structure shown in Listing 6.5. Although the logic is simple,
the coding is a little difficult to follow.

Listing 6.5 TicketPrice VBA Program

If (ticketPrice =20) Then
MsgBox "Standing at rear"


If (ticketPrice =30) Then
MsgBox "Front wing seats"


If (ticketPrice =40) Then
MsgBox "Lower front wing seats"


If (ticketPrice =50) Then
MsgBox " Balcony"


If (ticketPrice =60) Then
MsgBox "Upper Balcony"


MsgBox "Royal Box"

End If
End If
End If
End If
End If

VBA supports the Select Case statement that can make it easier to under-
stand, multiple-choice conditions than If…ElseIf…Else statements. The
syntax of the Select Case statement is as follows:

Select Case Expression
Case value

one or more VBA statements

Case value

one or more VBA statements

Case value

one or more VBA statements

Case Else

one or more VBA statements

End Select


Listing 6.6 is a Select Case version of Listing 6.5. Select Case organises the
multiple-choice selections into a more manageable format.

Listing 6.6 Using Select Case to simplify complex If…Else statements

Sub ticketSelect()
Dim ticketPrice As Integer
ticketPrice = InputBox("Input ticket price")
Select Case ticketPrice
Case 20: MsgBox "Standing at rear"
Case 30: MsgBox "Front wing seats"
Case 40: MsgBox "Front lower wing seats"
Case 50: MsgBox "Balcony"
Case 60: MsgBox "Upper Balcony"
Case Else: MsgBox "Royal Box"
End Select
End Sub

In this example, if ticketPrice holds 20, the message “Standing at rear” is
displayed. If it holds 30, “Front wing seats” is displayed. The logic continues
through the Case 60: statement. If ticketPrice holds a value that does not fall
in the range 20 to 60, the final Case Else displays the message “Royal Box”.

Figure 6.5 Screenshot of Select Case example


The body of each Case can consist of more than one statement, just as the
body of an If or If…Else can consist of more than one. VBA executes all the
statements for any given Case match until the next Case is reached. Once
VBA executes a matching Case value, it skips the remaining Case state-
ments and continues with the code that follows End Select.
Notice the colons after each Case value statement. These are optional, but
do help to separate the case being tested from the code that it executes.

Alternative select case formats

You can use variations on the above Case Select format as the following
syntax shows:

Select Case Expression
Case expr1a To expr2a:
One or more VBA statements
Case expr1b To expr2b:
One or more VBA statements
Case expr1c To expr2c:
One or more VBA statements

Case Else:

One or more VBA statements

End Select

The Case lines require a range, such as 4 To 6. The To option enables you
to match against a range instead of a relation or an exact match.

Notes on select case

The format of Select Case makes it look as difficult as a complex nested
If…Else, but Select Case structures are easier to code and to maintain than
their If…Else counterparts. Select Case is a good substitute for long, nested
If…Else conditions when several choices are possible.
The Case expression can be any VBA expression – such as a calculation, a
string value, or a numeric value – provided that it results in an integer or a
string value, and this must match the expression’s data type.
The Select Case structure is useful when you must make several choices
based on data values. It can have two or more Case value sections, and the
code that is executed depends on which value matches the expression. You
can write an (optional) Case Else body of code which will be executed if


none of the values match. Otherwise, nothing happens and control continues
with the statement that follows End Select.
Here is an example of a macro based on month_bonus range in the
weeklysales worksheet of the SALESMAN.XLS workbook The purpose of
the macro is to classify cells in the range weekTotal by assigning different
colours to the cells depending on whether the cell value is less than 60, equal
to 60, 61 to 70 or greater than 70. The colours then assigned would be green,
red, blue and yellow respectively. In this example (Listing 6.7), we can see
that the Select Case statement is used for this four-way selection.

Listing 6.7 Implementing a multi-way If using Select Case

Sub HighlightRanges ()
Dim myCell As Object
For Each myCell In Range ("weekTotal")
Select Case myCell.Value
Case Is < 60

myCell.Interior.ColorIndex = 4 'green

Case Is = 60

myCell.Interior.ColorIndex = 3 'red
Case 61 To 70
myCell.Interior.ColorIndex = 5 'blue

Case Is > 60

myCell.Interior.ColorIndex = 6 'yellow

End Select

End Sub


Avoid using Select Case when a simple If or If…Else will do the job. Unless you need

to compare against more than a couple of values, use If and If…Else statements because

of their simplicity. Use Select Case instead of the embedded If…Else when you have three

or more options, because it keeps the code much simpler and easier to maintain.


You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->