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
Date MarketQuantity
31-Jansweden2
25-Novus4
3-Julus5
15-Augcanada7
17-Augcanada3
17-Novcanada6
21-Augsweden8
25-Augus3
28-Augus6
28-Octus5
28-Augus1
26-Febsweden3
31-Augsweden4

<colgroup><col span="2"><col><col></colgroup><tbody>
</tbody>


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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
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,215,483
Messages
6,125,063
Members
449,206
Latest member
Healthydogs

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