Get maximum of comma-separated values in a cell

给你一囗甜甜゛ 提交于 2020-05-28 08:37:28

问题


In a cell I have comma-separated numbers. I would like to have the max value of these numbers.

eg: A1 = "2,5,1,4"

what should be the formula in B1 to return the value 5?


回答1:


Assuming A1 contains a list of positive integers between 1 and 999, separated by commas but with no spaces, you can use this formula to find the highest number present

=MATCH(1000,INDEX(FIND(","&ROW(INDIRECT("1:999"))&",",","&A1&","),0))

That searches for all numbers between 1 and 999 and MATCH finds the "position" of the last (MAX) of those which, because we start at 1, is the same as the number itself

This works for any amount of numbers within A1, in any order, as long as they are in the specified format




回答2:


You could use VBA function here:

Public Function MAXSPLIT(ByVal Text As String, ByVal Delimiter As String) As Double
    Dim TextArray() As String
    TextArray = Split(Text, Delimiter)
    Dim ValueArray() As Double
    ReDim Preserve ValueArray(UBound(TextArray))
    For I = LBound(TextArray) To UBound(TextArray)
        ValueArray(I) = CDbl(TextArray(I))
    Next
    ' You can use any other function here: Average, Min etc.
    MAXSPLIT = WorksheetFunction.Max(ValueArray)
End Function

Works with any number of values and any delimiter. Usage (you specify string of values and delimiter):

=MAXSPLIT("2,5,6,7,8.1,3.254",",")



回答3:


You might parse you data with comma as the delimiter and then take the max of the resulting pairs of cells, or you might apply a formula like:

=MAX(1*LEFT(A1,FIND(",",A1)-1),1*MID(A1,FIND(",",A1)+1,LEN(A1)))



回答4:


Unfortunately there is no built-in SPLIT function in Excel to split a string using a given delimiter, but you can write a quick wrapper for VBA's Split function:

Public Function SplitXL(ByVal s As String, Optional delim As String = " ") As String()
    SplitXL = Split(s, delim)
End Function

This is then usable in Excel formulas. Note that it returns an array of strings; to be maxable, these strings first need to be parsed as numbers using the VALUE function. This gives an array of numbers on which MAX can operate.

You can get your desired result using this array formula

=MAX(VALUE(SplitXL(A1,",")))

which, as any other array formula, must be entered using Ctrl Shift Enter.

enter image description here




回答5:


=MAX(SPLIT(A1, ","))

Max value from a csv string:



来源:https://stackoverflow.com/questions/29005956/get-maximum-of-comma-separated-values-in-a-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!