Returning an array of sums without any helper column

KolGuyXcel

Board Regular
Joined
Jun 29, 2018
Messages
147
We have n positive integral "random" values in n consecutive rows in a column with only the n-th value always as 0. Is there a formula which would return in a particular cell, without any helper column/row/cells, an array where the p-th term is the sum of the first p terms of the original set of values?

Using Excel 2013.
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
I can't think of a way to write this as a formula, but that doesn't mean it's not possible, and I suspect it may be by calculating an array in some way and then using Textjoin to merge it into one text string

The best I can come up with is a User-Defined Function (UDF) built using VBA and entered as a worksheet formula. It's not an ideal approach but may be of use to you

Code:
Public Function arrSums(rng As Range) As String

' add maximum cell limit to prevent long processing , overflow errors etc. Could also restrict formula to the worksheet Used range
If rng.Cells.Count > 100 Then Exit Function


Dim cl As Range, i As Integer, j As Integer
Dim arrResults(): ReDim arrResults(1 To rng.Cells.Count)


For i = 1 To rng.Cells.Count
    For j = 1 To i
        arrResults(i) = arrResults(i) + rng(j)
    Next j
Next i


arrSums = Join(arrResults, "; ")


End Function

Add this code to a standard VBA code module, then type "=arrSums(your range here)" as a normal Excel formula. You may need to add "Application.Volatile" as a new first row of code (after the function name row) in order to have it recalculate on demand
 
Last edited:
Upvote 0
Here are 2 formulas to do it:

BCD
311, 4, 10, 12, 13, 21, 32, 37, 37
431, 4, 10, 12, 13, 21, 32, 37, 37
56
62
71
88
911
105
110

<colgroup><col style="width: 25pxpx"><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Array Formulas
CellFormula
D3{=TEXTJOIN(", ",TRUE,MMULT(IF(ROW(B3:B11)>=TRANSPOSE(ROW(B3:B11)),TRANSPOSE(B3:B11),0),ROW(B3:B11)^0))}
D4{=TEXTJOIN(", ",1,SUBTOTAL(9,OFFSET(B3,0,0,ROW(B3:B11)-ROW(B3)+1)))}

<thead>
</thead><tbody>
</tbody>
Entered with Ctrl+Shift+Enter. If entered correctly, Excel will surround with curly braces {}.
Note: Do not try and enter the {} manually yourself

<tbody>
</tbody>



Both use the TEXTJOIN function, which is not available in Excel 2013. I do have a UDF called TEXTJOINx which duplicates the functionality of TEXTJOIN. But if you use it, then you have a macro-enabled workbook. And if that's the case, it may be preferable to use a custom macro such as baitmaster provided.
 
Upvote 0
Thanks for your responses.

However, UDF-s would need to enable macros which is a challenge. For most of the workstations.

Besides, these are returning "strings". How to reference these as "integer arrays"?
 
Upvote 0
Maybe you can explain what you'd use this for. You can't really put an array in a cell. You can do something like ={1,2,3} but you'd have to select 3 cells to show them all. If you want all of them to display, you need to create a string, which is what we did.

If you want to create an array that is usable by another formula, you could use the Name Manager. Click "Define Name", give it a name like MyArray, and use the formula:

=SUBTOTAL(9,OFFSET(Sheet1!$B$3,0,0,ROW(Sheet1!$B$3:$B$11)-ROW(Sheet1!$B$3)+1))

in the Refers to: box. Now you can use MyArray in any other formula where you'd want the array. You can select a vertical range , enter =MyArray in the top cell, and confirm with Control+Shift+Enter to just show the values.
 
Last edited:
Upvote 0
Solution
Nice formulas Eric, any chance you could explain the logic of what's happening here please? I was trying to use Row numbers within a Sumproduct but couldn't get it, I knew there was something that would work!
 
Upvote 0
The SUBTOTAL formula is actually pretty easy. It's an array formula, but you don't need to use Control+Shift+Enter since that's assumed when you enter a formula in the Name Manager. As a rule, you can't use OFFSET inside an array formula, but you can if it's inside SUBTOTAL. The key is the parameter within OFFSET that says how many rows are in the range. I just used the ROW(Sheet1!$B$3:$B$11)-ROW(Sheet1!$B$3)+1 part to create an array from 1 to n {1,2,3,4,5,...}. Then the SUBTOTAL sums up the individual ranges created by OFFSET of the various lengths. 9 is the parameter for SUBTOTAL that means "SUM".

I'll forego explaining the MMULT formula, since the SUBTOTAL one is so much easier. I only really presented it because it was the first one I got working, and I wanted to have something to show for my time! The techniques there are useful in some circumstances.


KolGuyXcel, I'm glad it works for you! Good job adapting it for your needs!
 
Upvote 0
Yep of course I get you now, should've been able to see that as it's the height that's changing each time - especially as I was looking to do something very similar in Sumproduct. Nice trick

No worries about the MMULT, I didn't enjoy matrices when I did my maths degree 20+ years ago, and I'm happy to continue not using them now!
 
Upvote 0

Forum statistics

Threads
1,214,375
Messages
6,119,170
Members
448,870
Latest member
max_pedreira

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