简体   繁体   中英

Excel Macro Autofilter and Delete Rows

I am trying to get the coding below working but getting errors.

Coding selects another worksheet, then autofilter a column and delete row.

Line having trouble is .AutoFilter 1, " NoNo "

Run-time error '1004': AutoFilter method of Range clas failed.

Can I please get some assistance.

Private Sub Project()
Worksheets("YesYes").Select
With ActiveSheet
.AutoFilterMode = False
 With Range("y1", Range("y" & Rows.Count).End(xlUp))
    .AutoFilter 1, "*NoNo*"
    On Error Resume Next
    .Offset(1).SpecialCells(12).EntireRow.Delete
End With
.AutoFilterMode = False
End With
End Sub

Selecting does not necessarily "Activate" the worksheet. Instead of:

Worksheets("YesYes").Select
With ActiveSheet

Do this:

Worksheets("YesYes").Activate
With ActiveSheet

99% of the times using Select or Activate is not necessary.

Instead use fully qualified objects, in your case use:

With Worksheets("YesYes")

And nested below use:

With .Range("y1", .Range("y" & .Rows.Count).End(xlUp))

And then add the rest of your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM