我意识到这很愚蠢,但我花了太多时间想办法解决这个问题。我只需要这个块循环,直到输入“红色”、“红色”、“蓝色”或“蓝色”;(为了简化可读性,我将脚本更改为颜色)。
如果有更好的办法,请详细说明。
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")发布于 2022-04-25 21:49:33
可以在Case语句中列出多个拼写:
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这不会处理"REd“、"ReD”、"reD“、"rED”和"rEd“值,因此,更好的防弹方法是像Geert Bellekens建议的那样,使用LCase或UCase函数来转换情况:
Select Case UCase(sColor)
Case "RED"
MsgBox "Color is Red"
Case "BLUE"
MsgBox "Color is Blue"
Case Else
MsgBox "Color not found"
End SelectStrComp函数可能是另一个使比较大小写不敏感的选项,但它不像选择Case块那样容易读取:
If StrComp(sColor, "RED", vbTextCompare) = 0 Then MsgBox "Color is Red"
If StrComp(sColor, "BLUE", vbTextCompare) = 0 Then MsgBox "Color is Blue"发布于 2022-04-20 19:29:39
我已经找到了答案;我意识到在代码的这一部分中避免使用case函数会更容易。新格式如下:
'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=1https://stackoverflow.com/questions/71943731
复制相似问题