11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 24: VBA Programming Concepts 651<br />

The Select Case construct<br />

The Select Case construct is useful for choosing among three or more options. this construct<br />

also works with two options and is a good alternative to using If-Then-Else. The syntax for<br />

Select Case is as follows:<br />

Select Case testexpression<br />

[Case expressionlist–n<br />

[instructions–n]]<br />

[Case Else<br />

[default_instructions]]<br />

End Select<br />

The following example of a Select Case construct shows another way to code the GreetMe<br />

examples presented in the preceding section:<br />

Function GreetMe()<br />

Select Case Time<br />

Case Is < 0.5<br />

GreetMe = “Good Morning”<br />

Case 0.5 To 0.75<br />

GreetMe = “Good Afternoon”<br />

Case Else<br />

GreetMe = “Good Evening”<br />

End Select<br />

End Function<br />

And here’s a rewritten version of the Discount function from the previous section, this time<br />

using a Select Case construct:<br />

Function Discount2(quantity)<br />

Select Case quantity<br />

Case Is = 75<br />

Discount2 = 0.25<br />

End Select<br />

End Function<br />

Any number of instructions can be written below each Case statement; they all execute if that<br />

case evaluates to TRUE.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!