鉴于
Select Case cmd
case "ONE": MsgBox "one"
case "TWO": MsgBox "two"
case "THREE": MsgBox "three"
End select
我的要求是,如果 cmd = "ONE"
,我需要显示 "one"
,然后显示"two"
,但目前我只能看到 "one"
,然后程序就跳出了选择语句...
鉴于
Select Case cmd
case "ONE": MsgBox "one"
case "TWO": MsgBox "two"
case "THREE": MsgBox "three"
End select
我的要求是,如果 cmd = "ONE"
,我需要显示 "one"
,然后显示"two"
,但目前我只能看到 "one"
,然后程序就跳出了选择语句...
Select Case cmd
case "ONE", "TWO":
if cmd = "ONE" THEN
MsgBox "one"
end if
MsgBox "two"
case "THREE": MsgBox "three"
End select
一些技术可以胜任这项工作:
If cmd = "ONE" Then
MsgBox "one"
cmd = "TWO"
End If
If cmd = "TWO" Then
MsgBox "two"
cmd = "THREE"
End If
If Cmd = "THREE" Then
MsgBox "three"
End If
你只能用较长的方式来完成它。
Select Case cmd
case "ONE": MsgBox "one"
MsgBox "two"
MsgBox "three"
case "TWO": MsgBox "two"
MsgBox "three"
case "THREE": MsgBox "three"
End select
Select
包装在 While
中,并在每个 case 结尾处指定下一步骤。While cmd <> ""
Select Case cmd
Case "ONE"
MsgBox "one"
cmd = "TWO"
Case "TWO"
MsgBox "two"
cmd = ""
Case "THREE"
MsgBox "three"
cmd = ""
End Select
Wend
这是故意设计的。http://msdn.microsoft.com/en-us/library/ee177199%28PROT.10%29.aspx
您可以尝试使用“goto”或过程调用来解决它。
我认为GoTo会很有效。
Select Case cmd
Case "ONE"
MsgBox "one"
GoTo AfterONE:
Case "TWO"
AfterONE:
MsgBox "two"
Case "THREE"
MsgBox "three"
End Select
它有效;我已经测试过。
If cmd = "ONE" Then
MsgBox LCase$(cmd)
MsgBox "two"
Else
Msgbox LCase$(cmd)
End If
在C/C++中,非空Case语句中的“Fall through”是错误的主要来源,因此大多数其他语言(包括VBA)都没有实现它。因此,如其他地方所述,请采用冗长的方式并考虑过程调用以避免重复。
然而,如果像我一样,您来到这个页面是因为您不记得如何进行空的drop-through,则可以对已接受答案进行轻微变化,即可以(与所有VBA命令一样)使用下划线表示继续到下一行:
Select Case cmd
Case "ONE", _
"TWO":
if cmd = "ONE" Then MsgBox "one"
MsgBox "two"
Case "THREE":
MsgBox "three"
End select