从不同长度/格式的单元格中提取数字

时间:2018-04-06 15:42:06

标签: excel excel-formula formula min

我正在寻找从A列中提取数字的公式。我遇到的问题是不同的长度和格式。我正在寻找基于下面所需结果列的解决方案。

B栏中的公式:

=IFERROR(INT(LEFT(REPLACE(SUBSTITUTE(A4,"-"," "),1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A4&1/17))-1,""),5)),INT(LEFT(REPLACE(SUBSTITUTE(A4,"-"," "),1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A4&1/17))-1,""),4)))

工作簿: enter image description here

谢谢!

3 个答案:

答案 0 :(得分:2)

尝试以下用户定义函数:

Public Function LastNumber(s As String) As Variant
    Dim L As Long, i As Long, temp As String, arr
    Dim CH As String

    L = Len(s)
    temp = ""
    For i = 1 To L
        CH = Mid(s, i, 1)
        If CH Like "[0-9]" Then
            temp = temp & CH
        Else
            temp = temp & " "
        End If
    Next i

    arr = Split(Application.WorksheetFunction.Trim(temp), " ")
    For i = UBound(arr) To LBound(arr) Step -1
        If IsNumeric(arr(i)) Then
            LastNumber = CLng(arr(i))
            Exit Function
        End If
    Next i
    LastNumber = ""

End Function

enter image description here

它将在字符串中返回最后一个数字(数字组)

用户定义函数(UDF)非常易于安装和使用:

  1. ALT-F11调出VBE窗口
  2. ALT-I ALT-M打开了一个新模块
  3. 粘贴内容并关闭VBE窗口
  4. 如果保存工作簿,UDF将随之保存。 如果您在2003年之后使用的是Excel版本,则必须保存 该文件为.xlsm而不是.xlsx

    删除UDF:

    1. 按上述方式调出VBE窗口
    2. 清除代码
    3. 关闭VBE窗口
    4. 从Excel使用UDF:

      = myfunction的(A1)

      要了解有关宏的更多信息,请参阅:

      http://www.mvps.org/dmcritchie/excel/getstarted.htm

      http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

      有关UDF的详细信息,请参阅:

      http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

      必须启用宏才能使其生效!

答案 1 :(得分:2)

在标准公共模块代码表中尝试此用户定义函数。

function lastNumber(str as string)
    dim i as long, tmp as string

    tmp = str

    for i=len(tmp) to 1 step-1
        if asc(right(tmp, 1))<48 or asc(right(tmp, 1))>57 then
            tmp = left(tmp, len(tmp)-1)
        else
            exit for
        end if
    next i

    for i=len(tmp) to 1 step-1
        if asc(mid(tmp, i))<48 or asc(mid(tmp, i))>57 then
            exit for
        end if
    next i

    lastNumber = mid(tmp, i+1)
end function

答案 2 :(得分:0)

您可以使用Regex,它需要引用Microsoft VBScript正则表达式:

scan 'tableName', FILTER=>"SingleColumnValueFilter ('column family','col1',=,'regexstring:.*hey.*')"