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

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,507
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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You are applying an array formula to the entire column. You could limit it to only the range that contains data:
You can also use the classic screenupdating = false

Rich (BB code):
Sub test()
    Dim myMonths, x, r As Range, temp
    Dim lr As Long
    
    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
          lr = Sheets("result").Range("A" & Rows.Count).End(3).Row
            If Sheets("result").Evaluate("count(if(month(a1:a" & lr & ")=" & x & ",1))") Then
                MsgBox .[j2] & " is already filtered": Exit Sub
            End If
        End If
    
        Application.ScreenUpdating = False
        
        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
        
        Application.ScreenUpdating = False
    End With
End Sub
 
Upvote 0
Solution
Another option with autofilter to know if the month exists

VBA Code:
Sub test_2()
    Dim myMonths, x, r As Range, temp
    Dim lr As Long, n As Long
    Dim shR As Worksheet
    
    Application.ScreenUpdating = False
    
    Set shR = Sheets("result")
    
    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
          lr = shR.Range("A" & Rows.Count).End(3).Row
          n = 20 + x
          shR.Range("A1:E" & lr).AutoFilter 1, n, 11, 0, , 0
          lr = shR.Range("A" & Rows.Count).End(3).Row
          shR.ShowAllData
          If lr > 1 Then
              MsgBox .[j2] & " is already filtered"
              Application.ScreenUpdating = False
              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
        
        Application.ScreenUpdating = False
    End With
End Sub
 
Upvote 0
Hi Dante ,
this is really better .!:)
by the way the second code show me compile error in this word AutoFilter in this line

VBA Code:
shR.Range("A1:E" & lr).AutoFilter 1, n, 11, 0, , 0
 
Upvote 0
Hi Dante ,
this is really better .!:)
by the way the second code show me compile error in this word AutoFilter in this line

VBA Code:
shR.Range("A1:E" & lr).AutoFilter 1, n, 11, 0, , 0
It was another option, it works for me.
But we are left with post #2 😅
 
Upvote 0
Do you have a sub called AutoFilter?
what do you mean?
you mean I have another macro called AutoFilter?
if it's so no I don't any have another macro except what I got by Dante.moreover I create new workbook and the problem is still continuing .

this is the error.
ERROR1.JPG

if you see this is strange case and you interest then I will upload file.
 
Upvote 0
what do you mean?
you mean I have another macro called AutoFilter?
Yes, that was exactly what I meant. Asked because I also don't get an issue with that line, and that error is common when you name a sub or a variable the same as a method or property VBA uses
 
Upvote 0
so the version could cause the problem?
I work for 2010
tomorrow I will work for 2016 at work to see what happens.
 
Upvote 0
The version shouldn't be an issue but give it a go
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,099
Members
453,021
Latest member
Justyna P

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