Error with .Offset(1, 0).EntireRow.Delete

时光怂恿深爱的人放手 提交于 2020-05-16 05:00:47

问题


Have read numerous posts on this with various suggestions, can't seem to get past this error.

I have a series of code blocks which look for various data in column I of my spreadsheet. If that data is found, then the row should be deleted. I want to do delete all but the header row, and so my blocks of code are as follows (1 example):

Sub Strip()
With Columns("I")
    .AutoFilter Field:=1, Criteria1:="=70-79%", VisibleDropDown:=False
    .Offset(1, 0).EntireRow.Delete
    .AutoFilter
    .AutoFilter
End With
End Sub

When I step through this, and get to the Offset line, it does select the proper criteria, however when stepping past that line, I get the error: Run-time error 1004: Application-defined or Object-defined error.

Any ideas?


回答1:


Like I mentioned in the comment, You need to change your column to a range.

Try this (Untested)

Sub Strip()
    Dim ws As Worksheet
    Dim lRow As Long

    Set ws = Sheet1

    With ws
        .AutoFilterMode = False

        lRow = .Range("I" & .Rows.Count).End(xlUp).Row

        Set Rng = .Range("I1:I" & lRow)

        With Rng
            .AutoFilter Field:=1, Criteria1:="=70-79%", VisibleDropDown:=False
            .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With

        .AutoFilterMode = False
    End With
End Sub



回答2:


With Columns("I")
...
.Offset(1, 0).EntireRow.Delete

You cannot offset down an entire column. Try the UsedRange.Columns("I"), or something that is sure to include your data but not an entire column. The UsedRange needs explicit qualification to some worksheet, so if your code is not in a worksheet code module, qualify it either to some explicit sheet or to ActiveSheet.

With ActiveSheet.UsedRange.Columns("I")


来源:https://stackoverflow.com/questions/44593830/error-with-offset1-0-entirerow-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!