VBA 有一个Select
语句,可以根据值选择要运行的代码(很像switch
C 风格的语言)。此语句的语法如下所示:
Select Case x
Case 1
'do one thing
Case 2
'do a different thing
End Select
Case
右后的目的是什么Select
?Select
或者,没有的含义是Case
什么?
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.
这只是Select Case
VBA中语句的语法要求:Select Case Statement
Select
没有Case
会导致编译错误。
如果没有各种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