Delete rows from Autofilter

Posted on 2013-12-22
Last Modified: 2013-12-24
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

Question by:RWayneH
  • 3
  • 2
LVL 48

Accepted Solution

Rgonzo1971 earned 500 total points
ID: 39734998

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


Author Comment

ID: 39735040
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-
LVL 48

Expert Comment

ID: 39735659

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


Author Comment

ID: 39736671
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?

Author Closing Comment

ID: 39738532
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-

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now