如何从 Excel 中的文本字符串中提取特定数字?

How to extract specific numbers from a text string in Excel?

我有一个看起来像这样的数据集

abcdefg-1004958-2019-服务

xyz-3104568-科技有限公司-UG32594

xxfgdrtg-GH267384-FO(1082564)-2016-软件

FO501117898-ahdndje-2016-服务

我需要从此数据集中提取 1004958、3104568、1082564、501117898。换句话说,必须返回任何大于 100000 的值。

有什么公式可以做到这一点吗?因为我有10000多条这样的条目,而且顺序不均匀,而且我也不能用text to columns。

任何帮助将不胜感激。谢谢!

使用Microsoft365,你可以试试:

B1中的公式:

=MAX(FILTERXML("<t><s>"&CONCAT(IFERROR(MID(A1,SEQUENCE(LEN(A1)),1)*1,"</s><s>"))&"</s></t>","//s[.*0=0]"))

您可以使用用户定义的函数。这是我想出的:

Function FunNumber(RngTarget As Range, Optional BlnReportAsNumber As Boolean = True, Optional BytPlacement As Byte = 1)
    
    'Declarations.
    Dim StrValue As String
    Dim BytPosition As Byte
    Dim StrString01 As String
    
    'Setting.
    StrValue = RngTarget.Cells(1, 1).Value
    
    'Covering each character in StrValue.
    For BytPosition = 1 To Len(StrValue)
        'Removing any non-numerica character from StrValue.
        If Not IsNumeric(Mid(StrValue, BytPosition, 1)) Then
            StrValue = Left(StrValue, BytPosition - 1) & " " & Right(StrValue, Len(StrValue) - BytPosition)
        End If
    Next
    
    'Removing leading and trailing spaces from StrValue.
    StrValue = Trim(StrValue)
    
    'Covering each character in StrValue but the last one.
    For BytPosition = 1 To Len(StrValue) - 1
        'Removing any multiple space.
        If Mid(StrValue, BytPosition, 2) = "  " Then
            StrValue = Left(StrValue, BytPosition - 1) & " " & Right(StrValue, Len(StrValue) - BytPosition - 1)
            BytPosition = BytPosition - 1
        End If
    Next
    
    'Setting FunNumber.
    FunNumber = ""
    
CP_Small_Number_Removed:

    'Covering each section of StrValue marked by a space.
    For BytPosition = 0 To UBound(Split(StrValue, " "))
        
        'Setting StrString01 as section of StrValue marked by space.
        StrString01 = Split(StrValue, " ")(BytPosition)
        
        'Checking for a number smaller than 100000.
        If Len(StrString01) < 6 Then
            
            'Removing the number smaller than 100000.
            Select Case BytPosition
                Case Is = 0
                    StrValue = Right(StrValue, Len(StrValue) - Len(StrString01) - 1)
                    GoTo CP_Small_Number_Removed
                Case Is < UBound(Split(StrValue, " "))
                    StrValue = Replace(StrValue, " " & StrString01 & " ", " ")
                    GoTo CP_Small_Number_Removed
                Case Is = UBound(Split(StrValue, " "))
                    StrValue = Left(StrValue, Len(StrValue) - Len(StrString01) - 1)
                    GoTo CP_Small_Number_Removed
            End Select
            
        End If
    Next
    
    
    'Setting FunNumber.
    FunNumber = Split(StrValue, " ")(BytPlacement - 1)
    
    'Resetting FunNumber as number if needed.
    If BlnReportAsNumber = True Then
        FunNumber = FunNumber * 1
    End If
    
End Function

将它放在一个模块中。假设您的字符串在单元格 A1 中。在另一个单元格中写入 =FunNumber(A1) ,您应该会获得所需的结果。如果您希望它以文本形式报告(可能前导零很重要),您可以编写 =FunNumber(A1,FALSE)。如果有多个大于 100000 的数字,而您想查找例如第二个,请写类似 =FunNumber(A1,,2) 的内容(您当然可以指定您想要的是字符串还是数字,同时指定您想要的是第二个或第三个或任何数字)。如果没有找到大于 100000 的数字,则函数 returns #VALUE 错误。

对于会使用 LibreOffice Calc 的人来说,答案就简单多了,因为 Calc 默认支持正则表达式:

=REGEX(A1;"\d{6,}")

问题是关于 Excel,但由于答案的复杂性差异如此之大,我不想隐瞒 LibreOffice 的答案。