冒号运算符:
是VBA中的语句分隔符。
但是,有谁知道为什么前三个示例有效而第四个示例(未注释时)会产生错误?
Option Explicit
Public Sub TestMe()
If 1 = 1 Then: Debug.Print 1
If 2 = 2 Then Debug.Print 2
If 3 = 3 Then:
Debug.Print 3
' Gives error:
' If 4 = 4 Then
' Debug.Print 4
'Other Examples, from the comments and the answers:
:::::::::::::::::::::::::::: '<-- This seems to be ok
If 5 = 5 Then Debug.Print "5a"::: Debug.Print "5b"
If 6 = 0 Then Debug.Print "6a"::: Debug.Print "6b"
If 7 = 0 Then:
Debug.Print 7 ' Does not have anything to do with the condition...
If 8 = 0 Then Debug.Print "8a"::: Debug.Print "8b" Else Debug.Print "8c"
End Sub
我认为困惑来自于3
。我们会认为3
and 4
应该表现得一样。实际上,3
等价于:
If 3 = 3 Then: (do nothing) 'an empty statement
Debug.Print 3 ' <-- This will be executed regardless of the previous If condition
要查看它,请更改3
进入这个:
If 3 = 0 Then:
Debug.Print 3 '<-- 3 will be printed! ;)
结论是,是的,:
确实是将许多语句合并在一行上
干得好@Vityata! :)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)