简体   繁体   中英

Split mixed string in excel

I have an intractable problem. There is this huge, long column at work which contains mixed strings with the following format:

ue6584
th45
hur4562243

So it is very irregular, the only regularity is that it starts with letters and ends with numbers. I need to split the strings in each cell so that:

ue6584             —> ue 6584
th45                 —> th 45
hur4562243     —> hur 4562243

So the cell splits into two columns, one column containing the letters only, the other the numbers only. So far, I am thinking this is impossible to do in excel.

Can anyone help please?

Thank you in advance, Dritan

Or you can use a simple trick with built-in functions:

  • =LEFT(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))-1) - for string part;
  • =RIGHT(A1,LEN(A1)-MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))+1) - for number part;

在此处输入图片说明

You will likely need VBA, so I have created a UDF (plus it gave me an excuse to play with RegEx).

First, add the RegEx reference to the VBEditor. See Step 1 from this post for how to do that.

Then add these to a Module in your workbook:

Function return_letters(ByVal target As Range)
Dim regEx As New RegExp

Dim pattern As String: pattern = "[0-9]"
With regEx
    .Global = True
    .MultiLine = False
    .IgnoreCase = False
    .pattern = pattern
End With

If regEx.Test(target) Then
    return_letters = (regEx.Replace(target, ""))
End If

End Function

Function return_numbers(ByVal target As Range)
Dim regEx As New RegExp
Dim pattern As String: pattern = "[a-zA-Z]"
With regEx
    .Global = True
    .MultiLine = False
    .IgnoreCase = False
    .pattern = pattern
End With

If regEx.Test(target) Then
    return_numbers = (regEx.Replace(target, ""))
End If

End Function

Finally, just call each function:

在此处输入图片说明

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