无法使用删除循环获取Range类的FindNext属性

我试图做一个简单的macros来search和删除整行。 不幸的是,我得到一个Set c= .FindNext(c)

 Sub DeleteHeaderError() With Worksheets(1).Range("a2:a10000") Set c = .Find("Create Time", LookIn:=xlValues) If Not c Is Nothing Then firstAddress = c.Address Do c.Select Rows(c.Row).DELETE Shift:=xlUp Set c = .FindNext(c) Loop While Not c Is Nothing And c.Address <> firstAddress End If End With End Sub 

当你刚刚删除它时,你不能很好地使用c作为范围参数来findnext一个。 试试简单:

 Sub DeleteHeaderError() With Worksheets(1).Range("a2:a10000") Set c = .Find("Create Time", LookIn:=xlValues) If Not c Is Nothing Then Do Rows(c.Row).DELETE Shift:=xlUp Set c = .Find("Create Time", LookIn:=xlValues) Loop While Not c Is Nothing End If End With End Sub 

顺便说一下,你不应该使用这个:

 Loop While Not c Is Nothing And c.Address <> firstAddress 

如果cNothing则尝试访问c.Address将导致错误。 (我知道它在所有的帮助文件的例子,但它仍然是错的!)