简体   繁体   中英

Excel function: Split string into horizontal Array by delimiter

this time I did find a solution for my problem in here already but I could not figure out how to use it.

So basically I want to convert strings like

"aa#b#ccc#1#2"

into a horizontal array like

{aa.b.ccc.1.2} 

to use them later on with Sumproduct. So this article seems to do the job:

Split a string (cell) in Excel without VBA (eg for array formula)

In there I found the formula:

TRIM(MID(SUBSTITUTE(A1,"#",REPT(" ",99)),(ROW(OFFSET($A$1,,,LEN(A1)-LEN(SUBSTITUTE(A1,"#",""))+1))-1)*99+((ROW(OFFSET($A$1,,,LEN(A1)-LEN(SUBSTITUTE(A1,"#",""))+1)))=1),99))

However this will return an vertical array like:

{aa;b;ccc;1;2}

When I combine this with Sumproduct like

Sumproduct((a5:a10)*(b5:b10=ABOVE FORMULA))

I only get the sum of "aa" but not the rest. I tried transpose(ABOVE FORMULA) but it did not do the job.

Can you help me out?

Many greetings, Peter

PS: another problem is that my numbers become strings but this is something I can handle

PPS: {aa.b.ccc.1.2} This type of array is what I see when I press F9 on the formula "{=b1:f1"}

Use ISNUMBER(MATCH()) instead:

Sumproduct((a5:a10)*(ISNUMBER(MATCH(b5:b10&"",ABOVE FORMULA,0))))

Even with SUMPRODUCT, you need to use Ctrl-Shift-enter instead of Enter when exiting edit mode.

Edit:

A slightly shorter and less volatile version of your formula:

=TRIM(MID(SUBSTITUTE(A1,"#",REPT(" ",99)),(ROW(INDEX(AAA:AAA,1):INDEX(AAA:AAA,LEN(A1)-LEN(SUBSTITUTE(A1,"#",""))+1))-1)*99+1,99))

so in total:

=SUMPRODUCT((A5:A10)*(ISNUMBER(MATCH(B5:B10&"",TRIM(MID(SUBSTITUTE(A1,"#",REPT(" ",99)),(ROW(INDEX(AAA:AAA,1):INDEX(AAA:AAA,LEN(A1)-LEN(SUBSTITUTE(A1,"#",""))+1))-1)*99+1,99)),0))))

And use Ctrl-Shift-enter instead of Enter when exiting edit mode.

在此处输入图片说明

As you can see, we are passing the vertical array to the MATCH:

在此处输入图片说明

Which results in the proper array of TRUE/FALSE to pass to SUMPRODUCT:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM