简体   繁体   English

在Excel中拆分混合字符串

[英]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. 到目前为止,我认为这不可能在excel中完成。

Can anyone help please? 有人可以帮忙吗?

Thank you in advance, Dritan 预先感谢您,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; =LEFT(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))-1) -用于字符串部分;
  • =RIGHT(A1,LEN(A1)-MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))+1) - for number part; =RIGHT(A1,LEN(A1)-MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))+1) -用于数字部分;

在此处输入图片说明

You will likely need VBA, so I have created a UDF (plus it gave me an excuse to play with RegEx). 您可能需要VBA,所以我创建了UDF(加上它为我提供了使用RegEx的借口)。

First, add the RegEx reference to the VBEditor. 首先,将RegEx引用添加到VBEditor。 See Step 1 from this post for how to do that. 有关操作,请参阅此帖子中的步骤1

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: 最后,只需调用每个函数:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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