Select Case condition - And - Or

Barboza Babcock

New Member
Joined
Jul 3, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I've looked at a bunch of code for the Select Case statement. Probably not able to do this.

Is there a way to evaluate whether to go into a case base on a criteria other than the case?


Code:
Dim strYes as string, strNo as string

strYes = "Yes"
strNo= "Do not go into this case" 

Select case strYes

   case strYes is = "Yes" AND strNo = "Yes"
       'Only get to here if the case selection criteria(strYes) and the value of the variable (strNo) are both set to yes.
       'In this example should not get here.

End Select

strYes="Don't go in"
strNo = "Yes"
  
Select case strYes
  Case strYes is = "Yes" or strNo ="Yes"
      'Should get in here if one or the other value is true. Even if the case selection value (strYes)
      'Probably not possible??
   End Select

I'm guessing that the overriding factor is whether strYes ="Yes", and you can't do what I want.
Before I edited the code to a bunch of if then elseif statements I thought I would put this out there.


As always, thanks for taking a look!
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Would this work for you?
VBA Code:
Sub test()
    Dim strYes As String, strNo As String
    strYes = "Yes"
    strNo = "Do not go into this case"
    If strYes = "Yes" And strNo = "Yes" Then
        '
    ElseIf strYes = "Yes" Or strNo = "Yes" Then
        'your code here
    End If
End Sub
 
Upvote 0
You might be looking for something like this:

VBA Code:
Sub test1()
    
    strYes = "Yes"
    strNo = "Do not go into this case"
    
    Select Case True
        Case (strYes = "Yes" And strNo = "Yes")
            a = 1
        Case (strYes = "Yes" Or strNo = "Yes")
            a = 2
        Case Else
            a = 3
    End Select
    
End Sub
 
Upvote 0
Eric W method is one good way. Another approach is to create 2 other variables to hold you, one each to hold your AND and OR logic results.

VBA Code:
Public Sub MT03()
    Dim strYes As String, strNo As String
    Dim boolAndResult As Boolean
    Dim boolORResult As Boolean
    
    strYes = "Yes"
    strNo = "Do not go into this case"
    boolAndResult = strYes = "Yes" And strNo = "Yes"
    
    Select Case boolAndResult
        Case True
            MsgBox "Only get to here if the case selection criteria(strYes) and the value of the variable (strNo) " & _
            "are both set to yes. In this example should not get here."
        Case Else
            MsgBox "Passed. In this example should get here."
    End Select
    
    
End Sub
 
Upvote 0
Because of what I am trying to do, I will switch the code to If then Else statements. Lot's of conditions and more straight forward. In this case (pun intended) I should not have used Select Case. It was only after the fact that I realized there were conditions besides the case select.

Instead of forcing a square peg into a round hole I'll use the more appropriate if then structure.

I'll copy both K and Eric W's method into my "How to do" notes. I hadn't thought of evaluating the 2 conditions into one True or False variable.

THANKS TO ALL!!!!!!!!!!
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top