PDA

View Full Version : [SOLVED:] Select case OR ?



Movian
06-29-2010, 07:10 AM
Hey,
i was toying with some minor tweaks to some code and found myslef in need of being able to use a select case stateemnt in the following situation


Select case ucase(variablename)
case "F16" OR "D16"
case "B16"
case "J16"
case else
end select

i wanted to do the same thing for the 2 fields but i didn't want to write a sub just to re use the few lines. My initial test show that i can't use the OR in case statements. Unless there is another way of doing it.

So for now i just declared a second case for D16 and copied the code over.

Any thoughts ? "Other that write a sub or a function"

Imdabaum
06-29-2010, 08:05 AM
Just use...



Select Case ucase(variablename)
Case "F16" , "D16"
Case "B16"
Case "J16"
Case Else
End Select



Tested and Approved by yours truly.


'Testing Purposes
Public Function TestSelect(sCase As String)
Select Case sCase
Case "F16", "D16"
MsgBox "Case1 works." & sCase
Case "A16", "B16", "C16"
MsgBox "Case2 works." & sCase
Case "J16", "E16"
MsgBox "Case3 works." & sCase
Case Else
MsgBox "No case match works." & sCase
End Select
End Function

Public Function Test()
Dim testCases As Variant
Dim iLoop As Integer
testCases = Array("A16", "B16", "C16", "D16", "E16", "F16", "G16", "H16", "I16", "J16")
For iLoop = 0 To UBound(testCases)
TestSelect (testCases(iLoop))
Next iLoop
End Function

Movian
06-29-2010, 09:26 AM
now im left wondering why that didn't come up in any of my searches....

thanks :)

Imdabaum
06-29-2010, 04:41 PM
Yeah, it's a nice feature.

Also if your test case is an integer, then you can do



Select Case testCase
Case 1 To 20
....
Case 21 To 40.. etc..
End Case