'VBS | Case - Selection issue
I realize this is silly but I've spent too much time trying to figure this out. I just need this block to loop until either "Red", "red", "Blue", or "blue" is typed; (I changed the script to colors to simplify readability).
If there's a better way to do this please elaborate.
mbb=MsgBox ("Hit No" ,4, "Start script")
If mbb=7 Then mbt=MsgBox ("Do you like Red or Blue?" ,32, "Choose color")
If mbt=1 Then SOM
Sub SOM
Do
ibb=InputBox ("Please type: Red or Blue")
Select Case ibb
Case "Red"
mbt=MsgBox ("Please explain your answer" ,32, "You chose Red")
Case "red"
mbu=MsgBox ("Please explain your answer" ,32, "You chose Red")
Case "Blue"
mbv=MsgBox ("Please explain your answer" ,32, "You chose Blue")
Case "blue"
mbw=MsgBox ("Please explain your answer" ,32, "You chose Blue")
Case Else
MsgBox "Please Type: Red or Blue"
End Select
Loop Until mbt OR mbu OR mbv OR mbw=1
End Sub
If mbt Or mbu=1 Then mbx=MsgBox ("Rouge" ,4, "Rouge")
If mbv Or mbw=1 Then mby=MsgBox ("Bleu" ,4, "Bleu")
Solution 1:[1]
You can list multiple spellings in your Case statements:
Dim sColor
sColor = "red"
Select Case sColor
Case "Red", "red", "RED"
MsgBox "Color is Red"
Case "Blue", "blue", "BLUE"
MsgBox "Color is Blue"
Case Else
MsgBox "Color not found"
End Select
This will not handle "REd", "ReD", "reD", "rED" and "rEd" values so a more bullet-proof approach is to convert the case using LCase or UCase functions as Geert Bellekens suggests:
Select Case UCase(sColor)
Case "RED"
MsgBox "Color is Red"
Case "BLUE"
MsgBox "Color is Blue"
Case Else
MsgBox "Color not found"
End Select
The StrComp function could be another option to make the comparison case-insensitive but it is not as easy to read as a Select Case block:
If StrComp(sColor, "RED", vbTextCompare) = 0 Then MsgBox "Color is Red"
If StrComp(sColor, "BLUE", vbTextCompare) = 0 Then MsgBox "Color is Blue"
Solution 2:[2]
I've managed to find an answer; I realized it would be easier to avoid using the case function for this part of my code. The new format is as follows:
'simplified
Option Explicit
Dim a, x, y, w, z
Do
a=InputBox ("Please select hot or cold" ,0, "Type: ""Hot"" or ""Cold"" ")
If a="hot" Then x=MsgBox ("Hot selected" ,64, "Test")
If a="cold" Then y=MsgBox ("Cold Selected" ,64, "Test")
If a="Hot" Then z=MsgBox ("Hot selected" ,64, "Test")
If a="Cold" Then w=MsgBox ("Cold selected" ,64, "Test")
Loop Until x Or y Or z Or w=1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | 99isfailure |
