Home » excel » excel – How to split text into columns until capital letter or number is met

excel – How to split text into columns until capital letter or number is met

Posted by: admin May 14, 2020 Leave a comment

Questions:

So here’s the problem, I have a table with equipment names where I want to split text into columns until capital letter or number is met.

Here’s how table looks like:

Table as-is

Here’s how it should like after splitting:

Table to-be

I was thinking using left function but dont know how to include capital letter condition!
Here’s what I tried:

 =LEFT(A2,SMALL(FIND(CHAR(ROW(INDIRECT("65:90"))),A2&"ABCDEFGHIJKLMNOPQRSTUVWXYZ"&"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"&"1234567890" ),2)-1)

It doesn’t work unfortunately…
Important note: capital Letters can be written both in cyrillic and latin

Also, is it possible possible to accomplish this goal easier in Power Query?

Thanks!

Edit: added my attempts

How to&Answers:

You can do this with Power Query.
I’m sure there is a cleaner way to do this, but SplitOnAnyDelimiter does not seem to have an option to split on just the first instance, and I don’t have time to write everything into concise M-code, so for now, I added some custom columns:

  • Strip off the first letter so as to avoid finding a Capital letter at the beginning.
  • Then find the position of the first occurrence any digit, capital Latin, capital Cyrillic letter
  • Return the part of the string before that position.
  • Return the part of the string after that position.
  • Delete the unwanted columns

The M-Code:

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "2nd", each Text.Middle([Name],1)),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Position", each Text.PositionOfAny([2nd],{"0".."9","A".."Z","А".."Я"},Occurrence.First)),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Equipment", each Text.Start([Name],[Position])),
    #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Custom", each Text.Middle([2nd],[Position])),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom3",{"Name", "2nd", "Position"})
in
    #"Removed Columns"

Source

enter image description here

Results

enter image description here

EDIT I suspect the following code might be more efficient, as the only columns added are the results columns:

let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],

#"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Equipment", each Text.Start([Name],
        Text.PositionOfAny(Text.Middle([Name],1),{"0".."9","A".."Z","А".."Я"},Occurrence.First))),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Part No", each Text.Middle([Name],
    Text.PositionOfAny(Text.Middle([Name],1),{"0".."9","A".."Z","А".."Я"},Occurrence.First)+1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Name"})
in
    #"Removed Columns"

Answer:

Here is a way without VBA/RegEx:

enter image description here

Formula in B1:

{=LEFT(A1,SMALL(IFERROR(MATCH(FullList,UNICODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1)))+1,1)),0),0),SUMPRODUCT(--(IFERROR(MATCH(FullList,UNICODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1)))+1,1)),0),0)=0))+1))}

Notice to enter as array through CtrlShiftEnter

Formula in C1:

=RIGHT(A1,LEN(A1)-LEN(B1))
  • Notice that there is a trailing space in column B.

  • FullList is a named range in another sheet refering to a list of UNICODES that stand for all capital Latin and Cyrillic letters plus numbers. So a list from 48-57, 65-90 and 1040-1071.