why the code is slow despite of using advanced filter with simple data

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,494
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello,
I don't know why the code is slow using advanced filter despite of my data is not big !
what happens if the data big , what if happens then?!
the code should filter data based on each month in J2 cell

VBA Code:
Sub test()
    Dim myMonths, x, r As Range, temp
    myMonths = "janfebmaraprmayjunjulaugsepoctnovdec"
    With Sheets("data")
        Set r = .[k1:k2]
        temp = r.Value: r.ClearContents
        x = Fix(InStr(1, myMonths, .[j2], 1) / 3 + 1)
        If x < 1 Then
            MsgBox "Invalid Entry", , .[j2]: Exit Sub
        Else
            If Sheets("result").Evaluate("count(if(month(a:a)=" & x & ",a:a))") Then
                MsgBox .[j2] & " is already filtered": Exit Sub
            End If
        End If
        r(2).Formula = "=month(a2)=" & x
        With .Cells(1).CurrentRegion
            .AdvancedFilter 1, r
            .Offset(1).Copy _
            Sheets("result").Range("a" & Rows.Count).End(xlUp)(2)
        End With
        If .FilterMode Then .ShowAllData
        r.Value = temp
    End With
End Sub
COPY FILTERD MONTH.xlsm
ABCDEFGHIJ
1CODEITEMTYPEORIGINQSEARCH
201/03/2020VB23VBGJHGFF100.000MAR
310/05/2020BN12FDSMMM10.000
411/03/2020CV69JKIVV20.000
522/03/2020AQ2KJJNN30.000
611/08/2020YU12BNBDD30.000
730/06/2020YU13BNBDD31.000
DATA
Cells with Data Validation
CellAllowCriteria
B2Custom=ISERROR(SEARCH("above";E7))



result when write month in J2 then will copy to the bottom in result sheet.
COPY FILTERD MONTH.xlsm
ABCDE
1CODEITEMTYPEORIGINQ
201/03/2020VB23VBGJHGFF100.000
311/03/2020CV69JKIVV20.000
422/03/2020AQ2KJJNN30.000
RESULT
Cells with Data Validation
CellAllowCriteria
B2Custom=ISERROR(SEARCH("above";E7))


I use advanced filter without loop should really fats that what I think.
I hope to experts help me to find efficient way using advanced filter to make fast and deal with big data(could be 20000 rows in DATA sheet).
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
The only error I got with that code (I ran the whole code this time ) with the data you posted wasn't on that line, it was on the line
VBA Code:
shR.ShowAllData
which I initially edited out

Not sure why the last 2
VBA Code:
Application.ScreenUpdating = False
are there
 
Upvote 0
The only error I got with that code (I ran the whole code this time ) with the data you posted wasn't on that line, it was on the line
VBA Code:
shR.ShowAllData
which I initially edited out

Not sure why the last 2
VBA Code:
Application.ScreenUpdating = False
are there
I don't surprise if the code works for Dante, you in the beginning .

I 'm not sure if the problem from EXCEL setting or windows system !
 
Upvote 0
Can you post the actual file as you have it and I'll look at it later in the day.
 
Upvote 0
Afraid I have run the code on the file you supplied and the only error I get is on the shR.ShowAllData line, I can't reproduce the error you are getting
 
Upvote 0
The Autofilter only used to have 5 arguments, but now has an additional 6th for SubField. Dante's code provides 6, so won't work on older versions.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,173
Members
452,615
Latest member
bogeys2birdies

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