John Walkenbachs array formula

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,907
Office Version
  1. 365
Platform
  1. Windows
Hi,

A friend and I are stumped in trying to work out how this calculates the sum of the each integer in a number. Is anybody able to explain to me how this works? I'm confused around the ROW(A1:OFFSET()) segment in particular.

Many thanks,
Jon
Book6
ABCD
17845
224
Sheet1
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
This:

ROW($A$1:OFFSET($A$1,LEN(ABS(A1))-1,0)

returns an array of numbers from 1 to the length of the absolute number in A1, in this case {1,2,3,4}. The MID function returns the number at that position in the absolute value of A1, in this case {7,8,4,5}. The SUM function sums that array to retyrn 24.
 
Upvote 0
The key is on the MID function:

MID(ABS(A1),ROW($A$1:OFFSET($A$1,LEN(ABS(A1))-1,0)),1)

1) The first argument ensures that value on A1 is the absolute value
2) The second argument is where the number should be extracted from. He uses the function ROW to return an array that starts at 1 and ends at the length of the absolute value of the number on A1.
3) The last argument extracts only only number at a time

At this points, he uses the function VALUE to convert the strings of numbers returned by the MID function and then sums the array.

My own version is:

=SUM(VALUE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1)))

This will only sum positive values. If your number were -7845, it'd return an error. I'll leave you to get it sorted :cool: If you can't solve, let me know and I'll give the answer.
 
Upvote 0

Forum statistics

Threads
1,215,328
Messages
6,124,299
Members
449,149
Latest member
mwdbActuary

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