【问题标题】:vba-excel meaning of <> (angled brackets or greater-than and less-than symbols)<>的vba-excel含义(尖括号或大于和小于符号)
【发布时间】:2011-06-30 16:34:54
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: excel find vba


    【解决方案1】:

    &lt;&gt; 运算符表示c.Address 不等于 firstAddress

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


    旁注,我认为您收到错误 91(对象变量或未设置块变量。)因为代码行 Loop While Not c Is Nothing And c.Address &lt;&gt; firstAddress 将始终尝试执行第二个条件(c.Address &lt;&gt; firstAddress),即使第一个( 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
    

    【讨论】:

    • 谢谢,我不知道该怎么称呼他们,所以很难搜索。我想搜索“vba 运算符”会有所帮助,但我想不出这样称呼它。这很有帮助。
    • @Charlie,欢迎您。 顺便说一句,在这个网站上(看到你是新人),如果一个答案有帮助,你点赞它(点击向上箭头),如果它解决了你的问题,你接受它(点击复选标记)。尽管如此,我很乐意提供帮助。
    • 至于为什么总是评估c.Address &lt;&gt; firstAddressVBA 不支持短路布尔运算符 :-) 不知道为什么,但就是这样。
    • @pst,是的,如果这是 VB.NET,那么短路运算符应该是 AndAlso
    • @Matt Spinelli 谢谢马特!我有点不确定这种区别。不过,我仍然需要赢得投票特权,所以一旦我到达那里,我会对乐于助人的人给予积极的反馈,从而更加歧视他人。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 2021-05-17
    相关资源
    最近更新 更多