简体   繁体   English

在Excel中删除数字前的文本

[英]Remove text before a number in Excel

I have to remove some text either before or after a number in Excel and sometimes both. 我必须删除Excel中数字之前或之后的某些文本,有时两者都删除。 ie

Dr Smith Surgery 154 which ever road

I need to split this up into Dr Smith Surgery 154 and which ever road . 我需要将其拆分为Dr Smith Surgery 154以及which ever road

Now some records only have 154 which ever road . 现在有些记录只有154 which ever road

Can anyone advise? 有人可以建议吗? I have a few thousands of records to adjust and the thought of doing this manually just makes me want to cry. 我有数千条记录需要调整,而手动执行此操作的想法只是让我想哭。

Select the cells you wish to parse and run this small macro: 选择要解析的单元格并运行此小宏:

Sub Parser()
    Dim s1 As String, r As Range, _
        L As Long, i As Long
    For Each r In Selection
    s1 = r.Text
    L = Len(s1)
    For i = L To 1 Step -1
        If IsNumeric(Mid(s1, i, 1)) Then
            r.Offset(0, 1).Value = Left(s1, i)
            r.Offset(0, 2).Value = Mid(s1, i + 1)
            GoTo dun
        End If
    Next i
dun:
    Next r
End Sub

Everything to the right of the last digit will be put in column C 最后一位数字右边的所有内容都将放在C列中
Everything else will be put in column B 其他所有内容将放在B列中

For example: 例如:

在此处输入图片说明

Suppose your value is in A1. 假设您的值在A1中。 Suppose you create a helper column in B1. 假设您在B1中创建一个帮助器列。 Column C1 is your formula for the left side. C1列是左侧的公式。 Column D1 is your formula for the right side. D1列是您右侧的公式。

In B1, replace all the numbers with a character your text is guaranteed not to contain, and then get the location of the first instance of that character. 在B1中,用保证您的文本不包含的字符替换所有数字,然后获取该字符的第一个实例的位置。 Here I chose pipe ("|"). 在这里,我选择了管道(“ |”)。 Formula for B1: B1的公式:

=SEARCH("|",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1, 1, "|"), 2, "|"), 3, "|"), 4, "|"), 5, "|"), 6, "|"), 7, "|"), 8, "|"), 9, "|"), 0, "|"))

Formula for C1: C1的公式:

=IFERROR(LEFT(A1,B1- 1), A1)

Formula for D1: D1的公式:

=IFERROR(MID(A1,B1, LEN( A1)-B1+1), "")

Should you elect not to use a helper column, you can create two giant formulas of doom with the following: 如果您选择不使用辅助列,则可以使用以下命令创建两个巨大的厄运公式:

Formula for left side: 左侧的公式:

=IFERROR(LEFT(A1,SEARCH("|",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1, 1, "|"), 2, "|"), 3, "|"), 4, "|"), 5, "|"), 6, "|"), 7, "|"), 8, "|"), 9, "|"), 0, "|"))- 1), A1)

Formula for right side: 右侧的公式:

=IFERROR(MID(A1,SEARCH("|",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1, 1, "|"), 2, "|"), 3, "|"), 4, "|"), 5, "|"), 6, "|"), 7, "|"), 8, "|"), 9, "|"), 0, "|")), LEN( A1)-SEARCH("|",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1, 1, "|"), 2, "|"), 3, "|"), 4, "|"), 5, "|"), 6, "|"), 7, "|"), 8, "|"), 9, "|"), 0, "|"))+1), "")

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

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