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

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,485
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).
 
Hi

See if this update to your Advanced Filter code resolves your issue

VBA Code:
Sub test()
    Dim m           As Variant
    Dim lr          As Long
    Dim rngCriteria As Range, rngCopyTo As Range
    Dim wsData      As Worksheet, wsResult As Worksheet
    
    '-----------------------------------------------------------------------------------------------------------------------------------
    '                                                           SETTINGS
    '-----------------------------------------------------------------------------------------------------------------------------------
    
    'set True to mask (hide) criteria formula result from view
    Const HideCriteria As Boolean = False
    '-----------------------------------------------------------------------------------------------------------------------------------
    
    'set object variables
    With ThisWorkbook
        Set wsData = .Worksheets("data")
        Set wsResult = .Worksheets("result")
    End With
    
    Set rngCopyTo = wsResult.Range("A1:E1")
    
    With wsData
        'copy headers to result sheet
        rngCopyTo.Value = .Range("A1:E1").Value
        'criteria range
        If Not .Cells(2, 11).HasFormula Then
            Set rngCriteria = .[k1:k2]
        Else
            'get next blank cell in criteria range
            Set rngCriteria = .Range("K1").Resize(.Columns(11).Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1)
        End If
        
        'get selected month index
        m = Application.Match(.[j2], Application.GetCustomListContents(xlMonth), 0)
        
        If Not IsError(m) Then
            'check if month already filtered
            lr = wsResult.Cells(wsResult.Rows.Count, "A").End(xlUp).Row
            If wsResult.Evaluate("count(if(month(a1:a" & lr & ")=" & m & ",1))") Then
                MsgBox .[j2] & " Is already filtered", 48, "Month Already Filtered"
                Exit Sub
                
            Else
                'add criteria to range end
                rngCriteria(rngCriteria.Cells.Count).Formula = "=month(A2)=" & m
                'mask criteria formula result
                rngCriteria.Font.ColorIndex = IIf(HideCriteria, xlNone, xlAutomatic)
                
                'filter to results sheet
                .Cells(1).CurrentRegion.AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rngCriteria, _
                CopyToRange:=rngCopyTo, Unique:=False
                
            End If
            
        Else
            'month name error - inform user
            MsgBox .[j2] & Chr(10) & "Invalid Entry", 48, "Error"
        End If
        
    End With
    
End Sub

Note the change in how the code works for the Criteria Range.

Updated code ADDS each month you want to filter to the range to build a permanent list of all months you require to be filtered to the results sheet.
I have given you the ability to mask (hide) the criteria list result if required.

I have only lightly tested on your sample data & seemed to work & does what you want.



Dave
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Thanks Dave for another choice.:)

I have only lightly tested on your sample data & seemed to work & does what you want.
not completely .
based on original code should arrange months from small to big .
duplicates dates for MAR month will be under each other of them consecutively even if they are not arranged in DATA sheet.

by the way I wanted how the code should be with old version as RoryA said parameters will be different . so I have really curiosity how the parameters changes with old version . I'm talking about post#3.
thanks.
 
Upvote 0
not completely .
based on original code should arrange months from small to big .

It was just an idea but I did not consider the sorting of the data as I only looked to see if could resolve your speed issue but this is only something you can determine with your real data.
Simple way to resolve sort order would be to sort the master data prior to running the code but if do not want to do that then just need to add bit of code to sort the result.

Dave
 
Upvote 0
Simple way to resolve sort order would be to sort the master data prior to running the code but if do not want to do that then just need to add bit of code to sort the result.
I know .:)
just I would inform you how code works.;)
 
Upvote 0

Forum statistics

Threads
1,223,101
Messages
6,170,116
Members
452,302
Latest member
TaMere

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