Delete Rows in Excel

rawilken
rawilken used Ask the Experts™
on
I have an accounting worksheet where the names of the account are populated using a lookup to a master data set. This lookup leaves the value 0 (zero) for any account number that does not exist in the master data set. So now I want code that will delete all rows where the value in the row for column A is 0 (zero).
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2008

Commented:
Assuming your data starts in cell A1, and your column A is formatted as General, you can use the following autofilter code:

with Cells(1,1).currentregion
.autofilter
.autofilter field:=1, criteria1:="0"
.offset(1).resize(.rows.count-1).specialcells(xlcelltypevisible).entirerow.delete
.autofilter
end with

Open in new window

If you want to do this again and again then the above code would be just fine.

If you need this only once then it would be more convenient to apply Data > Filter

and then filter out the zero values and delete them.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial