Data loss when deleting filtered rows

SammyCRX

New Member
Joined
Aug 15, 2016
Messages
34
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
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try
Code:
ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.Delete
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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