Looking for faster alternative to looping for determining SUM of array elements

mchac

Well-known Member
Joined
Apr 15, 2013
Messages
531
I have a sheet with a data range that's about 50,000x100. I load that into an array for testing and one of the tests is to see if a set of cells are all zero. Right now I'm doing this by looping through each array element (roughly from array(c,20) to array(c,100)) to see if the sum is zero. Depending on the answer the sub branches off.

Code:
        ACCTG_ENTRY_TOTAL = 0
        For c = 20 To COLUMN_OF_INTEREST_NUMBER - 1  'Column #20 is the first with Financial data
            ACCTG_ENTRY_TOTAL = ACCTG_ENTRY_TOTAL + DATA_ARRAY1(a, c)
        Next c


This is currently the slowest part of the routine by far.

Can anyone suggest a faster way to determine if a set of elements in an array are all zero rather than looping.
 
I have a sheet with a data range that's about 50,000x100. I load that into an array for testing and one of the tests is to see if a set of cells are all zero. Right now I'm doing this by looping through each array element (roughly from array(c,20) to array(c,100)) to see if the sum is zero. Depending on the answer the sub branches off.

Code:
        ACCTG_ENTRY_TOTAL = 0
        For c = 20 To COLUMN_OF_INTEREST_NUMBER - 1  'Column #20 is the first with Financial data
            ACCTG_ENTRY_TOTAL = ACCTG_ENTRY_TOTAL + DATA_ARRAY1(a, c)
        Next c


This is currently the slowest part of the routine by far.

Can anyone suggest a faster way to determine if a set of elements in an array are all zero rather than looping.
What about this...

IsAllZeroes = (WorksheetFunction.Max(DATA_ARRAY1) = 0)
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Thanks Rick.
I don't want to delete the negative elements but I guess the test would be if Min = Max = 0.
It's a 2d array and if all elements from roughly 'col' 20 to 100 are blank (or zero) then I can delete the row.
Does your idea work for something like that?
 
Upvote 0
Fazza, I took your idea to have the sub create a dynamic Sum formula, paste in a column, sort on that column, search for the range where the zeros start and end, then apply range.entirerow.delete.
Previously it took ~827 secs to run everyting. Now it takes ~63. So the part just examining for zeros shows an even greater improvement because the other parts of the code are unchanged.

Thanks!
 
Upvote 0
Thanks Rick.
I don't want to delete the negative elements but I guess the test would be if Min = Max = 0.
It's a 2d array and if all elements from roughly 'col' 20 to 100 are blank (or zero) then I can delete the row.
Does your idea work for something like that?
Maybe this will work for you then...

IsAllZeroes = (WorksheetFunction.Max(DATA_ARRAY1) = WorksheetFunction.Min(DATA_ARRAY1))
 
Upvote 0
using only one worksheet function
Code:
YourArray = Range("A1").Resize(50000, 100)
s = WorksheetFunction.SumSq(YourArray) = 0
MsgBox s
However, generating a test array of size 50,000 by 100 with all zeros by the testdata code
Code:
Range("A1").Resize(50000, 100) = 0
and doing some timing testing, it seems to me that a looping code like
Code:
Dim c, b As Boolean
YourArray = Range("A1").Resize(50000, 100)
For Each c In YourArray
    If Not c Then b = True: Exit For
Next c
MsgBox b
is the fastest by quite some margin. It also works (for me at least) on some massive arrays where the worksheet function approaches fail.
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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