Kelvin Stott
Active Member
- Joined
- Oct 26, 2010
- Messages
- 338
I have created the following custom function called CumArray(), which returns an ARRAY of running row and/or column totals from a RANGE of discrete values, which I then use within array functions in a worksheet, and it works very nicely, for example:
{=MyRange*CUMARRAY(MyRange)}
However, because it outputs an array rather than a range, I can't use the output of this function as the input into the same function, for example:
{=MyRange*CUMARRAY(CUMARRAY(MyRange))}
How can I revise my function so that it either takes in a range and gives out a range, or ideally, takes in an array and gives out an array, so that the output can be used as the input for the same function?
{=MyRange*CUMARRAY(MyRange)}
However, because it outputs an array rather than a range, I can't use the output of this function as the input into the same function, for example:
{=MyRange*CUMARRAY(CUMARRAY(MyRange))}
How can I revise my function so that it either takes in a range and gives out a range, or ideally, takes in an array and gives out an array, so that the output can be used as the input for the same function?
Code:
Function CumArray(MyRange, Optional Form As Integer = 0) As Variant
'Returns an Array of running row and/or column totals from a Range of discrete values
On Error GoTo ExitFunction
Set Fn = WorksheetFunction
Dim x() As Variant
Dim r, rMax As Integer
Dim c, cMax As Integer
rMax = MyRange.Rows.Count
cMax = MyRange.Columns.Count
ReDim x(1 To rMax, 1 To cMax)
For r = 1 To rMax
For c = 1 To cMax
Select Case Form
Case 0: x(r, c) = Fn.Sum(MyRange.Resize(r, c))
Case 1: x(r, c) = Fn.Sum(MyRange.Resize(r, c).Columns(c))
Case 2: x(r, c) = Fn.Sum(MyRange.Resize(r, c).Rows(r))
End Select
Next c
Next r
CumArray = x
ExitFunction:
End Function