Speed up of SumIf between two arrays (13k; 30) & (500, 30) based on double criteria - array loop

d3rowy

New Member
Joined
Mar 29, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Dear MrExcel Lads,

I'm looking for a way to speed up my macro. Current timing varies between 30s if array2 has 500 rows and 2minutes if array2 contains 1500rows.

Case:
- two arrays: arrayInput & arrayAdjustments
- arrayAdjustment is created from a template in one of the sheets and then filled with data by week/keyfigure
- product & keyfigure combinatation is not unique in arrayinput
- product & keyfigure combination is unique in arrayAdjusments
- there are more types of keyfigures in arrayInput than in arrayAdjusments, im interested only in summing up what is in arrayAdjustment
- arrayAdjustments is basically a pivot with additional filtering of keyfigure, but it need it as raw table to process and change later

My current approach is to do it via array loops, is there a faster way? Or a way to optimize my code?
(I'm using a couple of public functions in the code below, but hope the their name will be self explanatory)

VBA Code:
Sub adjSheetFill()


Dim timerAdj_fast As Single
timerAdj_fast = Timer()

Call sharedData.defineVariables

Dim numberColumnKeyFigure As Integer
Dim arrayInput As Variant, arrayKF As Variant, arrayAdjustment As Variant, result As Variant
Dim keyFigure As String, keyFigureInput As String, product As String
Dim rng As range, cell As range, sum As Double
Dim columnKeyFigureAdjustment As Integer, columnkeyFigureInput As Integer, phasing As Integer


arrayAdjustment = Worksheets(sheetAdjustments).range("A1").CurrentRegion.Value       'array with list of desired KF from workspace tab,

Dim r As Long, j As Long, i as Long

            arrayInput = createarrayInput
            arrayInput = addKeyColumnToArray(arrayInput)
            columnkeyFigureInput = findHeaderinArray(arrayInput, headerKeyFigures)
            columnKeyFigureAdjustment = findHeaderinArray(arrayAdjustment, headerKeyFigures)
            columnProductAdjustment = findHeaderinArray(arrayAdjustment, headerKey)
            phasing = columnkeyFigureInput - columnKeyFigureAdjustment

                      
    For r = LBound(arrayAdjustment, 1) + 1 To UBound(arrayAdjustment, 1)
            sum = 0

            keyFigure = arrayAdjustment(r, columnKeyFigureAdjustment)
            product = arrayAdjustment(r, columnProductAdjustment)

            For j = columnKeyFigureAdjustment + 1 To columnKeyFigureAdjustment + periodCountWeeks

            sum = 0

                For i = LBound(arrayInput, 1) + 1 To UBound(arrayInput, 1) - 1
                    If IsEmpty(arrayInput(i, columnkeyFigureInput)) Then
                    GoTo continue
                    End If

                sum = sum + arrayInput(i, j + phasing)
                arrayAdjustment(r, j) = sum

                Next i             

continue:

                sum = sum + arrayInput(i, j + phasing)
                arrayAdjustment(r, j) = sum

            Next j

    Next r


Worksheets(sheetAdjustment.range("A1:AD10000").ClearContents
Call pasteArray(arrayAdjustment, sheetAdjustment, "A1", 0)

Debug.Print "adjSheetFill_fast - end : " & Timer - timerAdj_fast

End Sub


Book1
ABCDEFGHIJKLMNOPQRSTUVW
1Product DescKey FigureW02 2023W03 2023W04 2023W05 2023W06 2023W07 2023W08 2023W09 2023W10 2023W11 2023W12 2023W13 2023W14 2023W15 2023W16 2023W17 2023W18 2023W19 2023W20 2023W21 2023W22 2023
2AppleFactor 13026.2526.2522.522.526.2522.522.522.522.522.522.522.522.522.522.522.522.522.522.522.5
3AppleFactor 23026.2526.2522.522.526.2522.522.522.522.522.522.522.522.522.522.522.522.522.522.522.5
4AppleStat Factor000000000000000000000
5AppleAdjusted Stat Factor25.59139625.59139625.59139627.54992328.33333528.33333528.33333528.18676231.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.141869
6AppleBaseline25.59139625.59139625.59139627.54992328.33333528.33333528.33333528.18676231.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.141869
7ApplePromotion
8AppleSales
9AppleSales Plan25.59139625.59139625.59139627.54992328.33333528.33333528.33333528.18676231.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.141869
10AppleSales Adjustments
11AppleDemand-12.172228-12.172228-12.172228-8.264976-8.5-8.5
12AppleConsensus Demand13.41916813.41916813.41916819.28494719.83333519.83333528.33333528.18676231.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.141869
13AppleCustomer Demand
14AppleTotal Demand4545454531.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.14186931.141869
15AppleActuals45454545
16AppleStock
17BananaFactor 14541.2537.533.7533.7537.53026.2545454545454545454545454545
18BananaFactor 24541.2537.533.7533.7537.53026.2545454545454545454545454545
19BananaStat Factor000000000000000000000
20BananaAdjusted Stat Factor42.4179542.4179544.38356344.38356344.38356344.38356344.38356335.536238.29787339.01041839.01041839.01041739.01041839.01041839.01041839.01041839.01041839.01041839.01041839.01041839.010417
21BananaBaseline42.4179542.4179544.38356344.38356344.38356344.38356344.38356335.536238.29787339.01041839.01041839.01041739.01041839.01041839.01041839.01041839.01041839.01041839.01041839.01041839.010417
22BananaPromotion
23BananaSales
24BananaSales Plan42.4179542.4179544.38356344.38356344.38356344.38356344.38356335.536238.29787339.01041839.01041839.01041739.01041839.01041839.01041839.01041839.01041839.01041839.01041839.01041839.010417
25BananaSales Adjustments
26BananaDemand0000
27BananaConsensus Demand42.4179542.4179544.38356344.38356344.38356344.38356344.38356335.536238.29787339.01041839.01041839.01041739.01041839.01041839.01041839.01041839.01041839.01041839.01041839.01041839.010417
28BananaCustomer Demand
29BananaTotal Demand45459022539.01041839.01041739.01041839.01041839.01041839.01041839.01041839.01041839.01041839.01041839.010417
30BananaActuals454590225
31BananaStock
32AppleFactor 1180165165172.5168.7590108.75127.5131.25135135135135135135135135135135135135
33AppleFactor 2180165165172.5168.7590108.75127.5131.25135135135135135135135135135135135135
34AppleStat Factor000000000000000000000
35AppleAdjusted Stat Factor155.806438155.806437179.572448179.572448179.572448179.572448179.572448150.562641188.558469188.558469188.558469188.558468188.558469188.558469188.558469188.558469188.558469188.558469188.558469188.558469188.558469
36AppleBaseline155.806438155.806437179.572448179.572448179.572448179.572448179.572448150.562641188.558469188.558469188.558469188.558468188.558469188.558469188.558469188.558469188.558469188.558469188.558469188.558469188.558469
37ApplePromotion
38AppleSales
39AppleSales Plan155.806438155.806437179.572448179.572448179.572448179.572448179.572448150.562641188.558469188.558469188.558469188.558468188.558469188.558469188.558469188.558469188.558469188.558469188.558469188.558469188.558469
40AppleSales Adjustments
41AppleDemand00-53.871734-53.871734-53.871734-53.871734
42AppleConsensus Demand155.806438155.806437125.700714125.700714125.700714125.700714179.572448150.562641188.558469188.558469188.558469188.558468188.558469188.558469188.558469188.558469188.558469188.558469188.558469188.558469188.558469
43AppleCustomer Demand0000
44AppleTotal Demand42.4179542.4179544.38356344.38356344.38356344.38356344.38356335.536238.29787339.01041839.01041839.01041739.01041839.01041839.01041839.01041839.01041839.01041839.01041839.01041839.010417
45AppleActuals
46AppleStock45459022539.01041839.01041739.01041839.01041839.01041839.01041839.01041839.01041839.01041839.01041839.010417
arrayInput


Book1
ABCDEFGHIJKLMNOPQRSTUVW
16Product DescKey FigureSum of W02 2023Sum of W03 2023Sum of W04 2023Sum of W05 2023Sum of W06 2023Sum of W07 2023Sum of W08 2023Sum of W09 2023Sum of W10 2023Sum of W11 2023Sum of W12 2023Sum of W13 2023Sum of W14 2023Sum of W15 2023Sum of W16 2023Sum of W17 2023Sum of W18 2023Sum of W19 2023Sum of W20 2023Sum of W21 2023Sum of W22 2023
17AppleActuals
18AppleDemand
19ApplePromotion
20AppleSales
21BananaActuals
22BananaDemand
23BananaPromotion
24BananaSales
arrayAdjustmentsEmpty





Book1
ABCDEFGHIJKLMNOPQRSTUVW
16Product DescKey FigureSum of W02 2023Sum of W03 2023Sum of W04 2023Sum of W05 2023Sum of W06 2023Sum of W07 2023Sum of W08 2023Sum of W09 2023Sum of W10 2023Sum of W11 2023Sum of W12 2023Sum of W13 2023Sum of W14 2023Sum of W15 2023Sum of W16 2023Sum of W17 2023Sum of W18 2023Sum of W19 2023Sum of W20 2023Sum of W21 2023Sum of W22 2023
17AppleActuals45454545
18AppleDemand-12.172228-12.172228-66.043962-62.13671-62.371734-62.371734
19ApplePromotion
20AppleSales
21BananaActuals454590225
22BananaDemand0000
23BananaPromotion
24BananaSales
arrayAdjustmentsFilled
 
Thank you for all the help, please get some rest :) As I though, I'm misunderstanding what the code is doing, but I will get there.
You help me tremendously, once again - thank you!
Happy to help, and thanks for the feedback 👍 😀
I strongly recommend investigating the use of Dictionaries for sumifs, countifs and averageifs. You won't regret it 😉
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Just quick update Kevin, managed to solve my problem using your solution based on dictionaries.
I made two dictionaries just to play around with them: one as you suggested and second that is pointing to position in "arrayAdjustments".

Final time for rows:
13k -> sumifs to 4k (initial 187s down to 1s)
13k -> sumifs to 0.5k (initial 23s down to 0.9s)

That's quite and impressive improvement, once again: thank you :)
 
Upvote 0

Forum statistics

Threads
1,215,330
Messages
6,124,310
Members
449,152
Latest member
PressEscape

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