VBA autofilter loop running too slowly

Status
Not open for further replies.

WolfOctober

New Member
Joined
Sep 2, 2017
Messages
9
Hi everybody,

Through trial-and-error, I have determined that the following section of my larger code is causing my overall macro to run really slowly:

Code:
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False

'Populating "HBsAg" with Average % IRR data    For j = 7 To 157
        For i = 1 To 77
            With Sheets("HBsAg")
                Sheets("Data Sheet").Select
                Columns("E:I").AutoFilter
                Sheets("Data Sheet").Range("E:I").AutoFilter Field:=3, Criteria1:="HBsAg"
                Sheets("Data Sheet").Range("E:I").AutoFilter Field:=1, Criteria1:=.Cells(j, 2)
                Sheets("Data Sheet").Range("E:I").AutoFilter Field:=5, Operator:=xlFilterValues, Criteria2:=Array(2, .Cells(6, i + 2))
                Dim filter_rng As Range
                Dim rw As Range
                Dim last_row As Long
                last_row = Cells(Rows.Count, "K").End(xlUp).Row
                Set filter_rng = Sheets("Data Sheet").Range("J1:K" & last_row)
                For Each rw In filter_rng.SpecialCells(xlCellTypeVisible)
                    Sheets("Test").Range("A" & rw.Row).Value = Sheets("Data Sheet").Range("J" & rw.Row).Value
                    Sheets("Test").Range("B" & rw.Row).Value = Sheets("Data Sheet").Range("K" & rw.Row).Value
                Next rw
                Worksheets("Data Sheet").AutoFilterMode = False
                If WorksheetFunction.CountA(Sheets("Test").Cells) = 0 Then
                    .Cells(j, i + 2) = vbNullString
                Else
                    .Cells(j, i + 2) = Application.WorksheetFunction.Sum(Sheets("Test").Columns("B:B")) / Application.WorksheetFunction.Sum(Sheets("Test").Columns("A:A")) * 100
                    .Cells(j, i + 2).NumberFormat = "0.000"
                End If
                Sheets("Test").Cells.Clear
            End With
        Next i
    Next j

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True

Basically, here's what the loop does --
1) Based on values in the "HBsAg" worksheet, the loop applies a unique autofilter to the "Data Sheet" worksheet
2) Transfers that filtered data into the "Test" worksheet
3) Runs a calculation (dividing the sum of one column by another) on that data that was just transferred to "Test"...
4) ...And prints (and formats) the result of that into the corresponding (based on the autofilters applied in Step 1) cell in "HBsAg"

When I run the macro, it takes 20 minutes to complete...

Is it because there are basically 10,000+ autofilters being applied to "Data Sheet" throughout the course of the loop? Could it be something else that's slowing things down? What can I do to speed things up?


Thanks in advance for any help you can give me!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi

Possibly this statement :-
Code:
                    .Cells(j, i + 2) = Application.WorksheetFunction.Sum(Sheets("Test").Columns("B:B")) / Application.WorksheetFunction.Sum(Sheets("Test").Columns("A:A")) * 100

Try to limit this calculation to the number of used rows.

hth
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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