Help with Autofilter macro

nirvehex

Well-known Member
Joined
Jul 27, 2011
Messages
503
Office Version
  1. 365
Platform
  1. Windows
Hey guys,

This seems easy enough, but I can't quite get it.

Here's the code:
Code:
Sub deletelines()
'
' deletelines Macro


    ActiveSheet.ListObjects("Table2").Range.AutoFilter Field:=1, Criteria1:= _
        "DELETE"
    Rows("2:2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    Range("Table2[[#Headers],[LineStatus]]").Select
    Selection.AutoFilter
End Sub

All I want to do is filter in column A for any line that says "DELETE" and select all the rows that do and delete them. Then unfilter.

Any ideas how to do this?

Thanks.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Code:
Dim sht as Worksheet
Dim rng As Range
Dim rngFiltered As Range

Set sht = ActiveSheet

With sht

    Set rng = .ListObjects("Table2").Range

    .AutoFilterMode = False

    With rng

        .AutoFilter Field:=1, Criteria1:="=DELETE"

        Set rngFiltered = .SpecialCells(xlCellTypeVisible)

        If rngFiltered.Rows.Count > 1 Then

            .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Delete

        End If

    End With

    .AutoFilterMode = False

End With
 
Upvote 0
Another option to @AOB, is one that I cam across that is faster than turning off autofilter, finding and deleting and then turning autofilter back on. It is a lot of code, but is really quick.

Code:
Sub DeleteDELETE()

    Dim Firstrow As Long
    Dim LastRow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim colDEL As Integer

        colDEL = 1 'Column Number
    
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With

    With ActiveSheet

        .Select

        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView

        .DisplayPageBreaks = False

        Firstrow = .UsedRange.Cells(2).Row
        LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

        For Lrow = LastRow To Firstrow Step -1

            With .Cells(Lrow, colDEL)

                If Not IsError(.Value) Then

                    If .Value = "DELETE" Then .EntireRow.Delete 'Looks for DELETE and deletes entire row

                End If

            End With

        Next Lrow

    End With

    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,869
Messages
6,122,012
Members
449,060
Latest member
LinusJE

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top