Evaluate long string (>255 characters) as array

Viewed 134

So I quite often find myself doing tasks on Excel which involve evaluating a text string as an array. Generally speaking I just use this:

Function EVAL(Ref As String)
    EVAL = Evaluate(Ref)
End Function

So the formula will be, for example:

=EVAL("{"&CHAR(34)&SUBSTITUTE(TEXTJOIN(";",TRUE,MID(Index[Industries],2,LEN(Index[Industries])-2)),";",CHAR(34)&";"&CHAR(34))&CHAR(34)&"}")

The cells in this example will have contents like:

;Automotive;Rail;Energy;
;Automotive;Rail;
;Energy;
;Automotive;Aerospace;

(As it happens this is the precise problem I'm stuck on right now, though it has come up in different ways in the past.)

This has worked for me in the past, but I've been running into difficulties lately.

I have come to the conclusion it isn't working because application.evaluate, it turns out, has a character limit of 255. I've seen examples of VBA tricks to bypass this for text strings that are formulas rather than arrays, but copy-pasting those they don't seem to work for when I'm using it to interpret a text string as an array rather than as a formula.

Is there some trick to get this to work? (Or, indeed, is there some alternative method to achieve this altogether?)

1 Answers

Right, as per my comments, if you are using ms365, you could avoid your workbook to be xlsm just because you need to split values into an array. Make use of what is available with native functions, for example:

enter image description here

Formula in C2:

=TEXTSPLIT(CONCAT(A1:A4),,";",1)

Formula in D2:

=FILTERXML("<t><s>"&SUBSTITUTE(CONCAT(A1:A4),";","</s><s>")&"</s></t>","//s[node()]")

Note 1: As per time of writing you'd need to enable the BETA-channel to gain access to TEXTSPLIT(), and if I recall correctly your version (2203) is allowed to start using this function. Just google how to get access and update your Excel.

Both options can obviously be nested inside the UNIQUE() function.


Note 2: If at any point CONCAT()'s limits are reached (32767 characters, thanks @ScottCraner), maybe you can avoid using that with help of the lambda's helper function REDUCE():

=TEXTSPLIT(REDUCE("",A1:A4,LAMBDA(a,b,a&b)),,";",1)

Note 3: In case you can't update your Excel just yet, and you wonder how to use FILTERXML(), don't mind me refering you to another post I wrote a while back here.

Related