我正在尝试制作一个简单的宏来搜索和删除整行。很遗憾,我在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
答案 0 :(得分:3)
当你刚刚删除它时,你可以很好地使用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
如果c
为Nothing
,则尝试访问c.Address
会导致错误。 (我知道它在所有帮助文件示例中,但它仍然是错误的!)