无法使用Delete Loop获取Range类的FindNext属性

时间:2015-07-08 13:59:16

标签: excel vba excel-vba

我正在尝试制作一个简单的宏来搜索和删除整行。很遗憾,我在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

1 个答案:

答案 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

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