Splitting text columns

邮差的信 提交于 2020-01-06 02:38:17

问题


I have a very large file (8 million rows) I imported in PowerPivot with an address column I want to split:

I tried LEFT and FIND but couldn't get the output as I wanted.


回答1:


With data in A1, in B1 enter:

=TRIM(MID(SUBSTITUTE($A1,"#",REPT(" ",999)),COLUMNS($A:A)*999-998,999))

and copy across.




回答2:


Text to Columns with # as the delimiter might suit, though possibly with TRIM for each of the resulting cells.




回答3:


Given that there is now(version 2010+) a menu command to do this, it can be done by the Import wizard, a VBA function does this easily, and other solutions have been posted, this may seem unnecessary. Nonetheless, here is an alternative approach using an array formula.

You need to have a header row and fill incremental integers (1, 2, 3, ...) in the columns where delimited values will be displayed. You need as many columns as there are delimited values. The delimiter used in this example is "#". A second delimiter ("@" in the example, but probably better is something like CHAR(1)) must be chosen for the formula.

It would be ideal if Excel had a Split function, like VBA, but it doesn't.

It would be handy if Excel had a FIND function which could find the n'th occurrence of a character, but it doesn't.

However, Excel does have the SUBSTITUTE function which can operate on the n'th occurrence of a character and then you can find that character. So a cumbersome workaround is possible.

=FIND("@",SUBSTITUTE($A2,"#","@",5))

will evaluate to the character position in the string in $A2 at which the 5th occurrence of the delimiter "#" occurs.

For example

$A2 = "Hello#my#name#is#Bob#What#is#your#name?" 

SUBSTITUTE($A2,"#","@",5) = "Hello#my#name#is#Bob@What#is#your#name?"

FIND("@",SUBSTITUTE($A2,"#","@",5)) = 21

To make it work more generally, pad the string with a delimiter on both ends and use the MID function to isolate the part of the text you want. Use the column header row (e.g. B$1:M$1) to specify which delimited value will appear in that column.

=MID($A2, FIND("@",SUBSTITUTE("#" & $A2 & "#","#","@",B$1)), FIND("@",SUBSTITUTE("#" & $A2 & "#","#","@",B$1+1)) - FIND("@",SUBSTITUTE("#" & $A2 & "#","#","@",B$1)) - 1 )

You can copy that formula across, or you can highlight across the columns first, and then type in the formula with the header array range in place of B$1 (e.g. B$1:F$1) and save it as an array formula with CTRL+SHIFT+ENTER.

For example,

{=MID($A2, FIND("@",SUBSTITUTE("#" & $A2 & "#","#","@",B$1:F$1)), FIND("@",SUBSTITUTE("#" & $A2 & "#","#","@",B$1:F$1+1)) - FIND("@",SUBSTITUTE("#" & $A2 & "#","#","@",B$1:F$1)) - 1 )}

would be the array formula for B2:F2 to split A2 into 5 separate #-delimited values, assuming that B1:F1 contains 1, 2, 3, 4, 5 across the cells.

Use FIND not SEARCH because SEARCH is not case sensitive and uses wildcards (*, ?).



来源:https://stackoverflow.com/questions/24182334/splitting-text-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!