How to make For Each Cumulative Sum more efficient

drpdrpdrp

New Member
Joined
Sep 9, 2021
Messages
22
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi All,
How can I make the following structure faster or simpler? Simple running cumulative sum of elements in an array returning an array of the same size. I've heard using For Each should make things faster but the lag I'm noticing with the structure below is noticeable on a few arrays per sheet (arrays are 100,000 rows each).


Function CumulativeSum(myArray As Variant) As Variant
Dim result() As Variant
Dim element As Variant
Dim i As Long

' Resize the result array to match the size of input array
ReDim result(1 To UBound(myArray))

' Initialize the cumulative sum variable
Dim sum As Double
sum = 0

i = 1

' Loop through each element in the array using For Each
For Each element In myArray
' Convert the element to a number (assuming the array contains numbers)
element = Val(element)

' Calculate cumulative sum
sum = sum + element

' Store the cumulative sum in the result array
result(i) = sum
i = i + 1
Next element

' Return the array of cumulative sums
CumulativeSum = result
End Function
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
What is the type of the "variant" you are calling this array with, Are you using a range as the input? If so this could be your problem
 
Upvote 1
I did a test with a million values and the result is immediate.

Do you have other macros running or does your sheet have too many formulas?
It takes a few seconds on my 4GB RAM pc when I run it for a few columns on the same sheet. Wondering if I can improve that. Thanks for your reply Dante
 
Upvote 0
What is the type of the "variant" you are calling this array with, Are you using a range as the input? If so this could be your problem
Correct. My input is an array (usually long... 100k to 200k elements)
 
Upvote 0
It takes a few seconds on my 4GB RAM pc when I run it for a few columns on the same sheet. Wondering if I can improve that. Thanks for your reply Dante
If you have the CumulativeSum function several times in the sheet, each time you do an execution, all the functions will be executed, that is, if you have 10 functions, each with 100 thousand records, then the function will be executed 10 times.
It is best if you put all 10 executions in a single macro and it is executed only once.
 
Upvote 1

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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