Extract maximum number from a string

前端 未结 4 887
灰色年华
灰色年华 2021-01-24 15:57

I am trying to extract all numbers from a string with a function in Excel. In the second time, I would like to extract the maximum value contains in the string.

4条回答
  •  长发绾君心
    2021-01-24 16:13

    You don't really need VBA for this if you have a version of Excel (2010+) that includes the AGGREGATE function, you can do it with a worksheet formula:

    =AGGREGATE(14,6,--TRIM(MID(SUBSTITUTE(SUBSTITUTE(A1,",",REPT(" ",99)),"=",REPT(" ",99)),seq_99,99)),1)
    

    where seq_99 is a Named Formula that refers to:

    =IF(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))=1,1,(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))-1)*99)
    

    The function results in an array, some of the values are numeric; the AGGREGATE function returns the largest value in the array, ignoring errors.

    The formulas below are for earlier versions of Excel and must be entered as array formulas, by holding down ctrl + shift while hitting enter If you do this correctly, Excel will place braces {...} around the formula.

    If you have 2007, you can use IFERROR

    =MAX(IFERROR(--TRIM(MID(SUBSTITUTE(SUBSTITUTE(A2,",",REPT(" ",99)),"=",REPT(" ",99)),seq_99,99)),0))
    

    For earlier versions, you can use:

    =MAX(IF(ISERROR(--TRIM(MID(SUBSTITUTE(SUBSTITUTE(A3,",",REPT(" ",99)),"=",REPT(" ",99)),seq_99,99))),0,--TRIM(MID(SUBSTITUTE(SUBSTITUTE(A3,",",REPT(" ",99)),"=",REPT(" ",99)),seq_99,99))))
    

提交回复
热议问题