<> 的 vba-excel 含义(尖括号或大于和小于符号)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6538079/
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
vba-excel meaning of <> (angled brackets or greater-than and less-than symbols)
提问by Charlie
I am working with find functions in VBA Excel, so when I ran into problems I pulled some example code from the help provided in Excel. I took their code that illustrates a basic find function and pasted it into a macro. On running the macro, I get a "Runtime error '91'" and the debugger highlights the line of code containing the angled brackets <>. These are the part of the code that I cannot understand.
我正在使用 VBA Excel 中的查找函数,所以当我遇到问题时,我从 Excel 中提供的帮助中提取了一些示例代码。我拿了他们的代码来说明一个基本的查找功能,并将其粘贴到一个宏中。在运行宏时,我收到“运行时错误 '91'”,调试器突出显示包含尖括号 <> 的代码行。这些是我无法理解的代码部分。
Can anyone tell me what these brackets represent?
谁能告诉我这些括号代表什么?
Sub exampleFindReplace()
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <> firstAddress
End If
End With
End Sub
回答by Matt
The <>
operator means c.Address
Is Not Equal TofirstAddress
.
该<>
操作装置c.Address
不等于firstAddress
。
In a C-style language this would be equivalent to c.Address != firstAddress
.
在 C 风格的语言中,这相当于c.Address != firstAddress
.
Side note, I think you are getting error 91 (Object variable or With block variable not set.) because the line of code Loop While Not c Is Nothing And c.Address <> firstAddress
will always try to execute the second condition (c.Address <> firstAddress
) even if the first (While Not C Is Nothing
) evaluates to false. Thus the call on c.Address will raise the exception.
旁注,我认为您收到错误 91(对象变量或未设置块变量。)因为即使第一个 ( ) 评估为假,代码行Loop While Not c Is Nothing And c.Address <> firstAddress
也会始终尝试执行第二个条件 ( c.Address <> firstAddress
) While Not C Is Nothing
。因此对 c.Address 的调用将引发异常。
Try writing the code like this as it will not allow that to happen:
尝试编写这样的代码,因为它不允许发生这种情况:
Sub exampleFindReplace()
With Worksheets(1).Range("a1:a500")
Set c = .Find(2, LookIn:=xlValues)
If Not c Is Nothing Then
firstAddress = c.Address
Do
c.Value = 5
Set c = .FindNext(c)
If c Is Nothing Then Exit Do
Loop While c.Address <> firstAddress
End If
End With
End Sub