Consulting

Results 1 to 4 of 4

Thread: Select case OR ?

  1. #1
    VBAX Mentor Movian's Avatar
    Joined
    Aug 2008
    Location
    NC, USA
    Posts
    399

    Select case OR ?

    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"
    Last edited by Aussiebear; 04-07-2023 at 01:25 PM. Reason: Adjusted the code tags
    "From the ashes of disaster grow the roses of success" - Chitty chitty bang bang

    "I fear not the man who has 10,000 kicks practiced once. I fear the man who has 1 kick practiced 10,000 times" - Bruce Lee

  2. #2
    VBAX Expert Imdabaum's Avatar
    Joined
    Jun 2006
    Posts
    652
    Location
    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
    Last edited by Aussiebear; 04-07-2023 at 01:26 PM. Reason: Adjusted the code tags
    Someday I'll understand everything...
    Even then...I'll still pretend I'm a beginner.

  3. #3
    VBAX Mentor Movian's Avatar
    Joined
    Aug 2008
    Location
    NC, USA
    Posts
    399
    now im left wondering why that didn't come up in any of my searches....

    thanks
    "From the ashes of disaster grow the roses of success" - Chitty chitty bang bang

    "I fear not the man who has 10,000 kicks practiced once. I fear the man who has 1 kick practiced 10,000 times" - Bruce Lee

  4. #4
    VBAX Expert Imdabaum's Avatar
    Joined
    Jun 2006
    Posts
    652
    Location
    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 Cas
    e
    Last edited by Aussiebear; 04-07-2023 at 01:27 PM. Reason: Adjusted the code tags
    Someday I'll understand everything...
    Even then...I'll still pretend I'm a beginner.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •