Data loss when deleting filtered rows

SammyCRX

New Member
Joined
Aug 15, 2016
Messages
32
Hi all,

I've got a macro which appeared to be working perfectly, however when I ran the data without any of the products to be excluded I found that I was losing EVERYTHING :confused: I've narrowed the culprit down to this:

Code:
    'Remove excluded products    Sheets("Consolidated").Select
    DataArea = Cells(Rows.Count, 1).End(xlUp).Row
    Range("A1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$S$" & DataArea).AutoFilter Field:=12, Criteria1:="1"
    Cells.Select
    Selection.Copy
    Sheets("Removed Products").Select
    Cells.Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Consolidated").Select


'Deletes removed products from Consolidated tab
With ActiveSheet.AutoFilter.Range
    .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
End With


'Remove autofilter
    Application.CutCopyMode = False
    Selection.AutoFilter

Or more specifically, it appears to be this:

Code:
With ActiveSheet.AutoFilter.Range    .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
End With

Please could someone advise if there's better code I could be using which won't result in everything being deleted should nothing match the criteria ("1") for products to remove? Any help would be much appreciated :)

Many thanks,
Sam
 

Some videos you may like

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,727
Office Version
  1. 365
Platform
  1. Windows
Try
Code:
ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.Delete
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,727
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,122,168
Messages
5,594,632
Members
413,919
Latest member
ZaxAlchemist

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
Top