删除过滤的行

时间:2019-11-21 14:41:26

标签: excel vba autofilter

概述:

我有2个宏,一个宏过滤数据,第二个宏删除标题行下方的可见行(标题行1-12)。

2个问题:

1)如何最好地将它们组合成一个宏? 2)如何获得第二个才能正常工作?

我收到

  

运行时错误1004:对象'_worksheet'的方法'Range'失败

在Delete()宏行上:

Set rng = .Range("A12:A" & LastRow).SpecialCells(xlCellTypeVisible)

我也尝试过:

Set rng = .Range("A12:A" & LastRow).SpecialCells(xlCellTypeVisible).cells

Sub Filter()
'filter and delete rows that have AW as FALSE

For Each sht In ThisWorkbook.Worksheets
    sht.Range("A12:AW12").autofilter Field:=49, Criteria1:="FALSE"
Next sht

End Sub


Sub Delete()

Dim sht As Worksheet, rng As Range, lastRow As Long
Set sht = Worksheets("Sheet1")

With sht
    lastRow = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
    Set rng = .Range("A12:A" & LastRow).SpecialCells(xlCellTypeVisible)
    rng.EntireRow.Delete
    .AutoFilterMode = False
End With

End Sub

2 个答案:

答案 0 :(得分:0)

Sub Filter()
    'filter and delete rows that have AW as FALSE

    Dim rng As Range

    For Each sht In ThisWorkbook.Worksheets

        LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

        sht.Range("A1:O1").AutoFilter Field:=9, Criteria1:="FALSE"

        sht.Range("A2:A" & LastRow).Delete

        If sht.AutoFilterMode Then
            sht.AutoFilterMode = False
        End If

    Next sht
End Sub

答案 1 :(得分:0)

这是可能会发现这种情况的任何人的有效代码。

Sub Filter23()
    'filter and delete rows that have AW as FALSE (working)
    Dim sht As Worksheet, rng As Range, lastRow As Long

    For Each sht In ThisWorkbook.Worksheets
        sht.Range("A12:AW12").autofilter Field:=49, Criteria1:="FALSE"
       With sht
        lastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        Set rng = sht.Range("A12:A" & lastRow).SpecialCells(xlCellTypeVisible)
        rng.EntireRow.Delete
       End With

    Next sht
End Sub