Excel VBA代码
问题描述:
我是新来编写VBA代码,并且在线下面遇到了这个代码,这与我正在尝试做的非常相似。但是,当我尝试运行它时,它会突出显示Loop While Not c Is Nothing And c.Address <> firstAddress
行,并弹出一条消息,提示“对象变量或块变量未设置”。Excel VBA代码
有谁知道如何解决这个问题?任何帮助深表感谢!
Sub Commodity()
'
' Commodity Macro
' Commodity
'
'
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
答
的问题还有就是你要寻找的2号和其更改为5。一旦它改变了所有的人都将继续从顶部循环中再次搜索,但c
将等于什么(如它无法找到任何东西)在第二次尝试评估c.Address <> firstAddress
时会导致错误。
当你要替换的人物我会删除部分,只留下Loop While Not c Is Nothing
在另一方面,如果你不改变价值观念,只是在寻找他们,你将需要包括c.Address <> firstAddress
阻止它陷入无限循环。
太棒了!感谢您的快速,有益的回应:) –