3

I've got a whole bunch of cells containing arbitrary length arrays stored as semicolon-delimited strings, ranging in length from 1 to 65 entries, like:

  • pcmsh15(232);pcmsh16(232);pcmsh17(136);
  • pcmsh12(40);
  • pcmsh12(40);
  • pcmsh12(5);pcmsh15(20);

I want a way to sum up the numbers in the parenthesis in Excel 2010 without using VBA, keeping in mind that they are arbitrary length strings, each contained in their own cell.

I've currently got a VBA function that I wrote that sums the numbers in parenthesis, but it's slowing my spreadsheet down. I know I can use Excel's SUBSTITUTE function to turn the semi-colon delimited arrays into something resembling Excel's internal array format, like

="{"&SUBSTITUTE([@[data]],";",",")&"}"

But unfortunately, Excel won't parse that as an array in SUM or COUNTIF, only as a string. One workaround I found makes a named range that references the cell with a string-formatted array, but since I also have an arbitrary number of these arrays in cells, I can't go naming every single cell.

Is something like this possible in "pure" excel functions?

9
  • When entering array formulas you don't type the { } so ="{"&...&"}" does not make it an array. To enter an array formula, tpye it without {} and press Ctrl-Shit-Enter Commented Jul 1, 2011 at 21:48
  • Are your "Arrays" in a single cell or several cells? ie is pcmsh15(232);pcmsh16(232);pcmsh17(136); in one cell or three? Commented Jul 1, 2011 at 21:51
  • Also to Chris Neilson's question, is there a limit to the number of terms that require summing within the string? There is a way as long as there is some idea on the number of terms. Commented Jul 2, 2011 at 1:42
  • I've edited my question with some clarification. @chris: the "arrays" are in one cell. @lionz: Most of the arrays are around 6 in length, but I've gotten some that have 65 entries, so I can't really depend on a function with a hard limit. The source data I have to work with comes from a CSV file, so that's why I have to deal with these weird arrays :/ Commented Jul 5, 2011 at 14:40
  • If your VBA function is slowing down your spreadsheet, then I dare to venture that it isn't optimised. Why don't you show it to us so we can fix that. Commented Jul 5, 2011 at 14:50

1 Answer 1

2

You can actually do this with no VBA involved. The method is kind of ridiculous, though. But, just for fun...

In cell B4 I put one of the strings you want to "parse":

B4:  pcmsh15(232);pcmsh16(232);pcmsh17(136);

Then I put the following formulas in the cells indicated:

B6:  =SUBSTITUTE(B4,"pcmsh", """dummy")
B8:  =SUBSTITUTE(B6,"(",""";")
B10: =SUBSTITUTE(B8,")","")
B12: =MID(B10, 1, LEN(B10)-1)
B14: ="{"&B12&"}"

That transforms the starting text into a text representation of a legal Excel array:

B14: {"dummy15";232;"dummy16";232;"dummy17";136}

You can make Excel evaluate that string and return an actual array with the following trick. Create a named formula that we'll call eval_left. Make sure cell C14 is selected when you create the named formula:

=EVALUATE(B14)

Note that the formula uses a relative reference, so if you use that name from a cell other than C14, you'll see that the B14 has changed to whatever cell is just to the left.

This uses the old Excel 4 macro function EVALUATE, which takes a string and returns the value that that string represents. Now we can put a final formula in cell C14:

C14: =SUM(eval_left)

Since SUM ignores non-numeric strings, you get the answer you want, with no VBA. (You do still get the macro warning though, because of the use of EVALUATE.)

Like I said, I consider this a just-for-fun curiousity. You're way better off using VBA for stuff like this.

EDIT:

In the unlikely event you wanted to actually do something like this, you'd run into the length limit of what EVALUATE can handle. You'd have to chop up your longer strings into less than 65 values, not use "dummy", etc., but it would still be doable.

Sign up to request clarification or add additional context in comments.

2 Comments

Huh, the evaluate() function exists in excel 2010 :( No fun for me, i guess. I'll probably be shackled to VBA for this particular problem then, but thanks for the info.
@blendmaster, the same functionality exposed by the Excel 4 macro function EVALUATE is also exposed by the VBA method Application.Evaluate. Again, just FYI...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.