Range.Find直接从MSDN不工作,应用程序定义或对象定义的错误

我已经坚持了几个小时,而且.find似乎在同一个程序的早期版本中为我工作。 我不确定在range.find工作的时候(2010或者2003),excel文件保存的是什么版本,但是现在Excel 2010没有工作。为了消除我的代码的其他部分作为源代码的错误,我从字面上复制和粘贴以下从MSDN到我的编辑器,把它放在它自己的子程序,声明的variables,并仍然得到相同的“应用程序定义或对象定义的错误”代码运行时。 这里是产生错误的代码,我已经注意到我偏离了什么是在MSDN上 :

Sub StraitFromMSDN() 'Only line that was changed: 'With Worksheets(1).Range("a1:a500") With ActiveSheet.Range("a1:a500") 'These two lines were added: Dim c As Range Dim firstAddress As String 'Error occurs on the following line: 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 

有谁知道为什么会这样呢? 我是相对较新的vba,所以有可能我缺less一些简单的东西。 提前致谢!

编辑:

当我把下面的部分拿出来的时候,我仍然得到错误:

 Sub StraitFromMSDN() Dim c As Range With ActiveSheet.Range("a1:a500") 'Error occurs here: Set c = .Find(What:=2, LookIn:=xlValues) End With End Sub 

 Sub StraitFromMSDN() Dim c As Range With ActiveSheet.Range("a1:a500") If Not .Find(What:=2, LookIn:=xlValues) Is Nothing Then 'test if there is any value you are looking for Set c = .Find(What:=2, LookIn:=xlValues) End If End With End Sub 

这只会发现一个包含值为2的单元格。如果您想查找所有这些单元格,请告诉我们,后面有一些更复杂的代码。

重新启动Excel修复了这个问题。 这是一个简单而简单的故障,不知道是什么引起了行为。 感谢所有帮助过我的人。