Deleting Rows From Filtered Range Via Macro

浪子不回头ぞ 提交于 2020-01-04 05:35:11

问题


I am having issues with the below code, which is trying to filter a set of data, then delete the visible rows. Currently I get a "Run Time 1004 Error: Delete Method or Range Class Failed" which occurs on the last line of my code. I found a similar question on the site, and the answer appears to be exactly what I have in my last line of code.

Dim LastRow As Long

LastRow = Worksheets("Orders").Range("A" & Rows.Count).End(xlUp).Row

Worksheets("Orders").Range("A1:CU" & LastRow).AutoFilter Field:=10,  Criteria1:="New"

If Worksheets("Orders").Range("A1:CU" & LastRow).SpecialCells(xlCellTypeVisible).Count > 1 Then

Worksheets("Orders").Range("A1:CU" & LastRow).Offset(1, 0).SpecialCells _
(xlCellTypeVisible).EntireRow.Delete

Later in my code, I filter the same set of data again, this time deleting the non-visible (filtered out) rows. I am also getting the same error on the last line of this code as well:

   Worksheets("Orders").Range("$A1:CC" & LastRow).AutoFilter Field:=26, Criteria1:= _
    "=*Jazz*", Operator:=xlAnd


Dim oRow As Range, rng As Range
Dim myRows As Range
With Sheets("Orders")
    Set myRows = Intersect(.Range("A:A").EntireRow, .UsedRange)
    If myRows Is Nothing Then Exit Sub
End With

For Each oRow In myRows.Columns(1).Cells
    If oRow.EntireRow.Hidden Then
        If rng Is Nothing Then
            Set rng = oRow
        Else
            Set rng = Union(rng, oRow)
        End If
    End If
Next

If Not rng Is Nothing Then rng.EntireRow.Delete

Any help on these errors would be much appreciated!


回答1:


You dont need to invoke the SpecialCells before deleting, because the method Delete acts only on the visible rows. The problem might be due to this.

Besides, the test .SpecialCells(xlCellTypeVisible).Count > 1 is useless because the header row of the filtered range remains visible.

Here's a safer and simpler way to write your code:

With Worksheets("Orders").UsedRange
  .AutoFilter 10, "New"
  .Offset(1).EntireRow.Delete        '     <--- no need for .SpecialCells
End With



回答2:


Post by @A.S.H is a great illustration of minimalism, but your 1st code block is still fine.

Just to make a small correction, change .SpecialCells(xlCellTypeVisible).Count > 1 to .SpecialCells(xlCellTypeVisible).Count > 2 as pointed out by him. Also, don't forget to add the line Worksheets("Orders").ShowAllData at the end.

The 2nd code block is unnecessarily complicated (and hence likely error-prone). Instead, simply change the filter of the 1st code block to Criteria1:="<>New" if you want to eliminate other than "New" items.

The Run Time Error issue seems to be due to something else. Make sure the sheet is not protected.

To see if it's the code or the file format that's causing the problem, you can also test the same code on a new workbook copying just the data (paste value) from your existing one and then run the code.




回答3:


You should remove any filters at the top of your code.

On Error Resume Next
If Worksheets("Orders").AutoFilter.FilterMode = True Then
   Worksheets("Orders").ShowAllData
End If


来源:https://stackoverflow.com/questions/45363077/deleting-rows-from-filtered-range-via-macro

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