Excel VBA 冒号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1725118/
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:59:25  来源:igfitidea点击:

Excel VBA Colon

excelvbaexcel-vba

提问by Raj More

Possible Duplicate:
VB Using colons to put two statements on same line

可能的重复:
VB 使用冒号将两个语句放在同一行

I have the following declaration in Excel VBA

我在 Excel VBA 中有以下声明

Public Const cdbArea = 1: Public Const cdbDist = 2: Public Const cdbChange1 = 4: Public Const cdbChange2 = 5: Public Const cdbTR = 5:
Public Const crbArea = 1: Public Const crbDist = 2: Public Const crbTerr = 3: Public Const crbChange1 = 4: Public Const crbTR = 5:
Public Const cdbWeek1 = 4

On first glance, the Colons look like separators, but I have never used this syntax before.

乍一看,冒号看起来像分隔符,但我以前从未使用过这种语法。

What are the Colons for?

冒号有什么用?

回答by BoltBait

You can put the statements on separate lines, if you like:

如果您愿意,可以将语句放在单独的行中:

Public Const cdbArea = 1
Public Const cdbDist = 2
Public Const cdbChange1 = 4

Or, you can separate them with colons as in your example above.

或者,您可以像上面的示例一样用冒号分隔它们。

回答by ray

Another use of the colon is to set specific variables to a calling statement:

冒号的另一个用途是为调用语句设置特定变量:

Option Explicit

Sub test()
    testWithOptions thirdParameter:="SecondParameterSkipped", firstParameter:="firstParam"
End Sub

Sub testWithOptions(firstParameter As String, Optional secondParameter As String, Optional thirdParameter As String)
    MsgBox "FirstParamter:  " & firstParameter & vbCrLf & _
                "ThirdParamter:  " & thirdParameter
End Sub

Notice the third parameter is set before the first and secondParameter (which is optional) is skipped all together.

请注意,第三个参数是在第一个和第二个参数(可选)一起跳过之前设置的。

I've only seen this used once in my career.

在我的职业生涯中,我只见过一次。

回答by aevanko

It's considered good practice to seperate statements like BoltBait said. The reasoning is that you can't enter break points in between 2 different statements if they are on the same line seperated by a colon.

像 BoltBait 所说的那样将语句分开被认为是一种很好的做法。原因是如果 2 个不同的语句位于由冒号分隔的同一行,则不能在它们之间输入断点。

Less lines of code does not equal faster, or better code ^^

更少的代码行不等于更快,或者更好的代码^^