我正在使用VBA Excel中的find函数,因此当我遇到问题时,我从Excel提供的帮助中提取了一些示例代码。我用他们的代码演示了一个基本的find函数,并将其粘贴到一个宏中。在运行宏时,我得到一个“运行时错误'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发布于 2011-07-01 00:38:37
<>运算符表示c.Address 不等于 firstAddress。
在C风格的语言中,这等同于c.Address != firstAddress。
附注,我认为你得到了错误91 (对象变量或块变量未设置)。因为代码行Loop While Not c Is Nothing And c.Address <> firstAddress将始终尝试执行第二个条件(c.Address <> firstAddress),即使第一个条件(While Not C Is Nothing)的计算结果为false。因此,对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 Subhttps://stackoverflow.com/questions/6538079
复制相似问题