vba-excel <> 的含义(尖括号或大于和小于符号)

发布于 2024-11-18 01:47:24 字数 505 浏览 1 评论 0 原文

我正在使用 VBA Excel 中的查找函数,因此当遇到问题时,我从 Excel 提供的帮助中提取了一些示例代码。我拿了他们的代码来说明基本的 查找函数并将其粘贴到宏中。运行宏时,我收到“运行时错误‘91’”,并且调试器突出显示包含尖括号 <> 的代码行。这些是我无法理解的代码部分。

谁能告诉我这些括号代表什么?

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

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2024-11-25 01:47:24

<> 运算符表示 c.Address 不等于 firstAddress

在 C 风格语言中,这相当于 c.Address != firstAddress


旁注,我认为您收到错误 91(未设置对象变量或 With 块变量。),因为代码行 Loop While Not c Is Nothing And c.Address <>即使第一个条件 (While Not C Is Nothing) 的计算结果为错误的。因此,对 c.Address 的调用将引发异常。

尝试编写这样的代码,因为它不会允许这种情况发生:

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

The <> operator means c.Address Is Not Equal To firstAddress.

In a C-style language this would be equivalent to 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.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文