本文介绍了VBA - 冒号`:`在VBA代码中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
冒号运算符:
是VBA中的语句分隔符。
The colon operator :
is a statement delimiter in VBA.
然而,有没有人有一个线索为什么前三个例子工作,第四个(当取消注释时)会产生错误?
However, does anyone has a clue why the first three examples work and the fourth (when uncommented) produces an error?
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
和 4
应该表现相同。实际上, 3
相当于:
I think the confusion comes from 3
. We'd think that 3
and 4
should behave the same. In fact, 3
is equivalent to this:
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! ;)
总之,是的,:
确实是在一行合并许多语句
In conclusion, yes, the :
is indeed to merge many statements on a single line
很好的工作@Vityata! :)
Good job @Vityata !!! :)
这篇关于VBA - 冒号`:`在VBA代码中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!