Single Cell Array Question!

migueljoa

New Member
Joined
Mar 27, 2018
Messages
4
Assume the following:

Cell A1 = 20
Cell A2 = 35
Cell A3 = 45
=SUM(A1:A3) = 100

But I want to have those 3 values within one single cell as an array like A4 ={"20";"35";"45"} and do
= SUM(A4). However, when I do this, the result = 20. When I read the cell value of A4 with VBA, Range(A4).Value = 20 and IsArray(Range(A4))=False. Meaning it only reads the first element of the array, and does not considers the cell as an array.

I've read solutions like inserting all the values in cells and naming the range. This does not work for me since my arrays could be quite extensive and I will have an array without a fixed length for each row in a table.

The only feasible solution I see is to input the whole array like a string inside a cell like = 20,35,45 and then create a User-defined-Function (UDF) SUM version that reads that string and converts the content to an Array. If the string has at least one comma, then the number of elements in the array would be the number of "," + 1.

Is there another/easier solution for this?


References:
https://www.excelforum.com/excel-formulas-and-functions/1182977-single-cell-array.html
https://www.ablebits.com/office-addins-blog/2015/02/25/array-formulas-functions-excel/
http://www.vbaexpress.com/forum/showthread.php?22816-Solved-Reading-one-cell-value-into-an-array
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about
Code:
Function SumAry(Cl As Range, Optional Delim As String = ",")
   Dim i As Long, s As Variant
   s = Split(Cl, Delim)
   For i = 0 To UBound(s)
      SumAry = SumAry + CDbl(s(i))
   Next i
End Function
Used like


Excel 2013/2016
AB
420,35,45100
51010
620;35;45100
Sheet2
Cell Formulas
RangeFormula
B4=SumAry(A4)
B5=SumAry(A5)
B6=SumAry(A6,";")
 
Last edited:
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
How about
Code:
Function SumAry(Cl As Range, Optional Delim As String = ",")
   Dim i As Long, s As Variant
   s = Split(Cl, Delim)
   For i = 0 To UBound(s)
      SumAry = SumAry + CDbl(s(i))
   Next i
End Function
Here is another way to write your function (no loop, one-liner)...
Code:
[table="width: 500"]
[tr]
	[td]Function SumAry(S As String, Optional Delim As String = ",")
  If Len(S) Then SumAry = Evaluate("SUM({" & Replace(S, Delim, ",") & "})")
End Function[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,986
Members
449,058
Latest member
oculus

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