我有一个公式,可以从单元格中的字符串中提取四位数的数字-现在我希望它提取第二个实例或多个实例

时间:2019-07-13 02:29:22

标签: excel excel-formula

编辑:我应该说,与输出所有四位数数字一样好或更好的是一个计数功能,该功能可以对四位数不间断数字进行计数。

而且,无论是数字输出还是计数,我都需要将公式向下推几千行。谢谢所有到目前为止回答的人,感谢您的帮助!


此公式可找到字符串中的第一个四位数数字-但我需要找到四位数数字的第二个实例,或者最后一个,或全部生成。只是不是第一个。需要调整的有用公式,谢谢您的考虑:=MID(D2,FIND("----",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(D2,"0","-"),"1","-"),"2","-"),"3","-"),"4","-"),"5","-"),"6","-"),"7","-"),"8","-"),"9","-")),4)

我已经尝试过根据以下主题的最高答案调整的公式:Excel: How to extract only the 6 digit number present in a cell string?

对于单元格中的此文本:“测试1军事时间1982 2008 2009 203401”

此公式:=MID(D2,FIND("----",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(D2,"0","-"),"1","-"),"2","-"),"3","-"),"4","-"),"5","-"),"6","-"),"7","-"),"8","-"),"9","-")),4)

生成以下内容:“ 1982”

我希望它输出 2008 1982,2008,2009,2034 ,或仅输出第一个结果 1982 < / strong>。

3 个答案:

答案 0 :(得分:0)

D3 中输入:

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(D2,"0","-"),"1","-"),"2","-"),"3","-"),"4","-"),"5","-"),"6","-"),"7","-"),"8","-"),"9","-")

D4 中输入:

=MID($D$2,FIND("xxxx",SUBSTITUTE($D$3,"----","xxxx",ROWS($1:1))),4)

并向下复制:

enter image description here

答案 1 :(得分:0)

几种方法。

既然您说2034年是可以接受的,那么您是否可以查找从右到左的第一个space,然后取下4个数字?如果该空间规则成立(那么这是一个简短的简短规则,它将返回 2034

=LEFT(SUBSTITUTE(RIGHT(SUBSTITUTE(D2, " ",REPT("?", 999)), 999),"?",""),4)

如果要在第一个4位数字之后(如您在 2008 中所述):    =MID($D$2,FIND("!!!!!",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(D2,"0","-"),"1","-"),"2","-"),"3","-"),"4","-"),"5","-"),"6","-"),"7","-"),"8","-"),"9","-"),"----","!!!!!",2)),4)

或者这是使用VBA的基本功能:

Function fDigitOutup(xString As String) As String
Dim p As Long, foundFour As Boolean, ftext As String

ftext = Replace(xString, " ", "")


    p = 4
    Do While Len(fDigitOutup) < 4 And p < 5000

        If Not foundFour Then
            If IsNumeric(Mid(ftext, p - 3, 4)) Then
                foundFour = True
            End If
        Else
            If IsNumeric(Mid(ftext, p, 1)) Then
                fDigitOutup = fDigitOutup & Mid(ftext, p, 1)
            End If
        End If

        p = p + 1
    Loop


End Function

答案 2 :(得分:0)

由于2034是可接受的返回值,因此,如果您具有带有TEXTJOIN函数的Excel 2016+,则可以使用以下 Array 公式:

=TEXTJOIN(", ",TRUE,LEFT(FILTERXML("<t><s>" & SUBSTITUTE(A1," ","</s><s>")& "</s></t>","//s[number()>=1000]"),4))

如果真的只想要1000-9999范围内的值,则可以使用以下 Array 公式:

=TEXTJOIN(", ",TRUE,FILTERXML("<t><s>" & SUBSTITUTE(A1," ","</s><s>")& "</s></t>","//s[number()>=1000 and number()<=9999]"))

由于上述是 array 公式,因此您需要在按下的同时按住 ctrl + shift 来“确认”它们。如果正确执行此操作,则Excel将按照公式栏中的说明在公式周围放置括号{...}

enter image description here

如果没有TEXTJOIN函数,则可以使用INDEX函数一次输出值1。例如,要在相邻列的单独单元格中输出这些值:

=IFERROR(INDEX(LEFT(FILTERXML("<t><s>"&SUBSTITUTE($A1," ","</s><s>")&"</s></t>","//s[number()>=1000]"),4),COLUMNS($A:A)),"")

并尽可能填写正确的内容:

enter image description here