2

VBA 有一个Select语句,可以根据值选择要运行的代码(很像switchC 风格的语言)。此语句的语法如下所示:

Select Case x
    Case 1
        'do one thing
    Case 2
        'do a different thing
End Select

Case右后的目的是什么SelectSelect或者,没有的含义是Case什么?

4

3 回答 3

5

There is no such thing as a Select statement in VBA. There is a Select Case statement.

Therefore, strictly speaking, the answers to your questions are: There is none, and there is none.

It's a bit like asking what is the meaning of Pri without nt and vice-versa... There is no particularly exciting answer to that question.

于 2012-05-02T20:52:02.673 回答
2

这只是Select CaseVBA中语句的语法要求:Select Case Statement

Select没有Case会导致编译错误。

于 2012-05-02T19:42:26.057 回答
1

如果没有各种Case语句,VBA 编译器将无法区分您希望针对每种情况执行的代码。

想象一下没有 case 语句的代码:

Select Case x

     'do one thing

     'do a different thing
End Select

你怎么知道什么时候做哪件事?

您可以使用 If 语句执行与 Select Case 语句相同的操作

If x Then
     'do one thing
ElseIf x Then
     'do a different thing
End If

如果我们去掉 If 语句会怎样?它不会按照你想要的方式工作。

正如其他人所说,Case 语句只是 VBA 的必要部分。

下面的链接很好地概述了 Select Case 语句以及一些示例:Select Case

于 2012-05-02T21:16:31.670 回答