You might try using the Select Case function - its almost
limitless. Select Case will work with either Numeric or
Text (String) data types.
Function Discount(Qty As Integer) As Single
Select Case Qty
Case Is <5
Discount = 0
Case 6 to 10
Discount = .025
Case 11 to 24
Discount = .1
Case Else
Discount = .125
End Select
End Function
or
Function ZipCode(State As String) As String
Select Case State
Case < "C"
ZipCode = "Who Cares"
Case = "Massachusetts"
ZipCode = "MA"
Case Else
ZipCode = "* * E R R O R * *"
End Select
End Function
Any number of instructions can be created below each Case
test, and those instructions will execute if the test
returns True.
Structure of test can be like:
Case Is < 0
Case 4
Case 4 To 11
Case 5 To 11, 14, 23, 37, 45, 46, 52 To 99
(Note that you must have these in numeric order left-to-
right and same is for Strings, alphabetically left-to-
right)
Case Is > 15
(This test would only activate (be True) if the number
fed into the Select Case is greater than 15 but not 23,
37, 45, 46, 52 to 99 of the immediate above test.)
To make this run a bit faster place your most likely True
conditions (tests) first. VBA starts at the top looking
for a match.
ALWAYS build in a Case Else as the last test just in case
something is wrong with your code OR you want a "just in
case nothing matches or exceeds" as I showed above in the
Discount example where Cas Else Discount = .125