如何通过 Excel VBA 中的 Select Case 失败?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1556910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:52:43  来源:igfitidea点击:

How to fall through a Select Case in Excel VBA?

vbaexcel-vbaoffice-2007excel

提问by Kevin Boyd

Given

给定的

Select Case cmd

    case "ONE":   MsgBox "one"

    case "TWO":   MsgBox "two"

    case "THREE": MsgBox "three"

End select

My requirement is if cmd = "ONE"I need "one"and then "two"displayed however currently I am getting "one"displayed and then the program is breaking out of the select case...

我的要求是如果cmd = "ONE"我需要 "one"然后"two"显示但是目前我正在"one"显示然后程序打破了选择案例......

回答by manji

Select Case cmd
    case "ONE", "TWO":   
                  if cmd = "ONE" THEN
                      MsgBox "one"
                  end if
                  MsgBox "two"

    case "THREE": MsgBox "three"

End select

回答by Kevin Boyd

Some if could do the job:

一些如果可以完成这项工作:

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

回答by Lance Roberts

You'll just have to do it the long way.

你只需要长途跋涉。

Select Case cmd

    case "ONE":   MsgBox "one"
                  MsgBox "two"
                  MsgBox "three"

    case "TWO":   MsgBox "two"
                  MsgBox "three"

    case "THREE": MsgBox "three"

End select

回答by JimmyPena

Why use Select Case for this? All you're doing is printing the lower-case version of whatever value 'cmd' is.

为什么要为此使用 Select Case?您所做的就是打印任何值 'cmd' 的小写版本。

If cmd = "ONE" Then
  MsgBox LCase$(cmd)
  MsgBox "two"
Else
  Msgbox LCase$(cmd)
End If

回答by Stephen Fluharty

GoTo would work well, I think.

GoTo 会很好用,我想。

Select Case cmd

    Case "ONE"
        MsgBox "one"
        GoTo AfterONE:

    Case "TWO"
AfterONE:
        MsgBox "two"

    Case "THREE"
        MsgBox "three"

End Select

It works; I tested it.

有用; 我测试了它。

回答by unDeadHerbs

I also had this problem recently.

我最近也有这个问题。

I found the most readable and scale-able solution was to create a basic state-machine.

我发现最具可读性和可扩展性的解决方案是创建一个基本的状态机。

Simply wrap the Selectin a Whileand end each case with which case is next.

只需将Selecta包裹起来,While然后以下一个案例结束每个案例。

While cmd <> ""
Select Case cmd
    Case "ONE"
        MsgBox  "one"
        cmd = "TWO"
    Case "TWO"
        MsgBox  "two"
        cmd = ""
    Case "THREE"
        MsgBox  "three"
        cmd = ""
End Select
Wend

回答by Michael Todd

That is by design. http://msdn.microsoft.com/en-us/library/ee177199%28PROT.10%29.aspx

那是设计使然。http://msdn.microsoft.com/en-us/library/ee177199%28PROT.10%29.aspx

You could try using 'goto' or procedure calls to get around it.

您可以尝试使用“goto”或过程调用来绕过它。