Filtering data based on two criteria's and then deleting those rows

harvey121

New Member
Joined
Nov 27, 2018
Messages
20
Hi all,

I'm searching for a macro wherein i can filter up to 5000 rows of data based on two conditions and then delete the filtered rows.

An example of what I'm trying to do is as follows:

A sample of my data looks like this:

ColA ColB ColC
[TABLE="width: 342"]
<colgroup><col span="2"><col><col></colgroup><tbody>[TR]
[TD]Date[/TD]
[TD] [/TD]
[TD]Market[/TD]
[TD]Quantity[/TD]
[/TR]
[TR]
[TD="align: right"]31-Jan[/TD]
[TD][/TD]
[TD]sweden[/TD]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD="align: right"]25-Nov[/TD]
[TD][/TD]
[TD]us[/TD]
[TD="align: right"]4[/TD]
[/TR]
[TR]
[TD="align: right"]3-Jul[/TD]
[TD][/TD]
[TD]us[/TD]
[TD="align: right"]5[/TD]
[/TR]
[TR]
[TD="align: right"]15-Aug[/TD]
[TD][/TD]
[TD]canada[/TD]
[TD="align: right"]7[/TD]
[/TR]
[TR]
[TD="align: right"]17-Aug[/TD]
[TD][/TD]
[TD]canada[/TD]
[TD="align: right"]3[/TD]
[/TR]
[TR]
[TD="align: right"]17-Nov[/TD]
[TD][/TD]
[TD]canada[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD="align: right"]21-Aug[/TD]
[TD][/TD]
[TD]sweden[/TD]
[TD="align: right"]8[/TD]
[/TR]
[TR]
[TD="align: right"]25-Aug[/TD]
[TD][/TD]
[TD]us[/TD]
[TD="align: right"]3[/TD]
[/TR]
[TR]
[TD="align: right"]28-Aug[/TD]
[TD][/TD]
[TD]us[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD="align: right"]28-Oct[/TD]
[TD][/TD]
[TD]us[/TD]
[TD="align: right"]5[/TD]
[/TR]
[TR]
[TD="align: right"]28-Aug[/TD]
[TD][/TD]
[TD]us[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD="align: right"]26-Feb[/TD]
[TD][/TD]
[TD]sweden[/TD]
[TD="align: right"]3[/TD]
[/TR]
[TR]
[TD="align: right"]31-Aug[/TD]
[TD][/TD]
[TD]sweden[/TD]
[TD="align: right"]4[/TD]
[/TR]
</tbody>[/TABLE]


Now I want to delete all those rows where the Market is Sweden and date is less than 21st March 2018, us and date less than 16th Oct 2018 canada and date less than 23rd Oct 2018.

PS: I'm getting the dates with the following format: 5/31/2018 11:23:51 PM but using short date over here.

Your help guys is really appreciated on this.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this:

Code:
Dim lr As Long
Dim arr As Variant

Application.ScreenUpdating = False

arr = Array("Sweden", DateSerial(2018, 3, 21), "US", DateSerial(2018, 10, 16), "Canada", DateSerial(2018, 10, 23))
With Sheets("Sheet1")
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    If lr < 2 Then Exit Sub
    For i = LBound(arr) To UBound(arr) Step 2
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Range("A1:C" & lr)
            .AutoFilter
            .AutoFilter Field:=1, Criteria1:="<" & CDbl(arr(i + 1)) + 1
            .AutoFilter Field:=2, Criteria1:=arr(i)
            If .SpecialCells(xlCellTypeVisible).Count > 1 Then
                .Offset(1, 0).Resize(lr - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            End If
        End With
        .AutoFilterMode = False
    Next
End With

Application.ScreenUpdating = True
 
Upvote 0
I tried this but I'm getting an error staing that "No cells were found' and the data is filtered out showing no rows.

Try this:

Code:
Dim lr As Long
Dim arr As Variant

Application.ScreenUpdating = False

arr = Array("Sweden", DateSerial(2018, 3, 21), "US", DateSerial(2018, 10, 16), "Canada", DateSerial(2018, 10, 23))
With Sheets("Sheet1")
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    If lr < 2 Then Exit Sub
    For i = LBound(arr) To UBound(arr) Step 2
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Range("A1:C" & lr)
            .AutoFilter
            .AutoFilter Field:=1, Criteria1:="<" & CDbl(arr(i + 1)) + 1
            .AutoFilter Field:=2, Criteria1:=arr(i)
            If .SpecialCells(xlCellTypeVisible).Count > 1 Then
                .Offset(1, 0).Resize(lr - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            End If
        End With
        .AutoFilterMode = False
    Next
End With

Application.ScreenUpdating = True
 
Upvote 0
Ive just thought. Change this line:

Code:
If .SpecialCells(xlCellTypeVisible).Count > 1 Then

to

Code:
If .SpecialCells(xlCellTypeVisible).Count > 3 Then

because we are using A:C so three columns. If all that is left is the headers then the filters have filtered everything out and there are no visible cells hence the error.
 
Upvote 0
Hi Steve,
The error is resolved but none of the rows are getting deleted.

My code looks like this:
Code:
Sub Macro1()



Dim lr As Long
Dim arr As Variant


Application.ScreenUpdating = False


arr = Array("sweden", DateSerial(2018, 3, 21), "us", DateSerial(2018, 10, 16), "canada", DateSerial(2018, 10, 23))
With Sheets("Sheet1")
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    If lr < 2 Then Exit Sub
    For i = LBound(arr) To UBound(arr) Step 2
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Range("A1:C" & lr)
            .AutoFilter
            .AutoFilter Field:=1, Criteria1:="<" & CDbl(arr(i + 1)) + 1
            .AutoFilter Field:=2, Criteria1:=arr(i)
            If .SpecialCells(xlCellTypeVisible).Count > 3 Then
                .Offset(1, 0).Resize(lr - 1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
            End If
        End With
        .AutoFilterMode = False
    Next
End With


Application.ScreenUpdating = True


End Sub

My data looks as follows:
https://ibb.co/QY4m397
QY4m397

QY4m397
 
Upvote 0
Its going to be the dates more than likely. If you click a cell with a date in it what is seen in the formula bar?
 
Upvote 0
Yeah formatting and the true value can be, and often is, different with dates. What does:

=ISNUMBER(A2)

produce? With A2 housing a date.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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