问题
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