Delete rows from Autofilter

Here is some code that the VBA recorder captured.  How would I use this on other different filters thoughout project?  I want to apply a filter and delete to results getting rid of the absolute values that the recorder places in them.  Ideally I would like to place some code right after the autofilter.  Can this be done?  Please advise and thanks. -R-

    ActiveSheet.Range("$A$1:$O$1195").AutoFilter Field:=1, Criteria1:="=-", _
        Operator:=xlOr, Criteria2:= _
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp

Open in new window

Who is Participating?
Rgonzo1971Connect With a Mentor Commented:

pls try

Sub Macro()
    ActiveSheet.UsedRange.AutoFilter Field:=1, Criteria1:="=-", _
        Operator:=xlOr, Criteria2:= _
    LastRow = Cells(Cells.Rows.Count, 1).End(xlUp).Row
    LastColumn = Cells(1, Cells.Columns.Count).End(xlToLeft).Column
    Range(Range("A2"), Cells(LastRow, LastColumn)).SpecialCells(xlCellTypeVisible).Delete Shift:=xlUp
End Sub

Open in new window

RWayneHAuthor Commented:
Hey thanks!!
Forgive me but I would like to use this in other places and I need to understand how to enter into this... like select the column first?  Can you help me read this?

In Ln 2 you are telling it too turn on AutoFilter
In Ln 3,4,5,6 sets the filter... but how does it how that it is set in column A?  Is that declared by the field #
In Ln 7   ???  Defines LastRow, however what is the 1).End(xlUp.Row mean?
In Ln 8   ???  Defines LastColumn,  what does .End(xlToLeft).Column mean?  Is no left of ColA
In Ln 9, 10   ???  Starting in A2, delete the rows that are visible between LastRow, LastColumn?  -R-

you are right

Line 3:
Field:=1 defines the column (in that case A)

if your filter were from B1 to D1, field:=1 would be column B

Line 7:in XL 2010 Cells(Cells.Rows.Count, 1) defines the cell A1048576
I use Cells.Rows.Count because the number of rows is different between XL 2003 and XL 2007 and further

.End(xlUp) is like doing Ctrl-Arrow Up so i find the last non Empty Cell in the Column

.Row gives me the row number

RWayneHAuthor Commented:
Because this does not use an autofilter (which is fine) is leaves me with another issue.  I was going to change the autofilter to:

    ActiveSheet.UsedRange.AutoFilter Field:=3, Criteria1:= _
    ActiveSheet.UsedRange.AutoFilter Field:=2, Criteria1:="=*SVC*", _
        Operator:=xlOr, Criteria2:="=*SRV*"

Then replace whatever was in column C (if anything) when "SERVICE PART"..
These will not be blank or have a space... but there was a couple other places that I was going to use this..  Any idea's.  Perhaps I need to do all the replacements without autofilters too?
RWayneHAuthor Commented:
Worked great, will repost on trying to change a different column value based on a filter instead of deleting the results of the filter.  Thanks. -R-
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.