+ Reply to Thread
Results 1 to 16 of 16

Find largest number in string containing multiple numbers

Hybrid View

  1. #1
    Registered User
    Join Date
    05-25-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    26

    Find largest number in string containing multiple numbers

    Say if I had a cell which contained:

    35.328, 32.086, 33.326, 34.479, 34.268, 33.953, 32.497, 31.37, 30.963, 29.252, 29.152, 28.655
    (all in one cell)
    and the cells do not always necessarily have exactly this amount of numbers in them, but up to a maximum of 24 different numbers.

    How would I be able to return the largest number in each cell??

    Thanks for any help!

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Find largest number in string containing multiple numbers

    Split the cell using Text to columns, then use the MAX function.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Find largest number in string containing multiple numbers

    you could try a user-defined function (I'm assuming there are only commas, spaces and numbers in the text string):

    Function MaxValue(rRange As Range) As Variant
    Dim MaxVal As Variant, ThisVal As Variant, TxtString As String, sn As Variant, en As Variant
    sn = 1 'start character
    MaxVal = "" 'maximum value
    If rRange.Value <> "" And rRange.Value <> " " Then
        TxtString = Replace(rRange.Value, ",", "")
        TxtString = Trim(TxtString)
        For en = 1 To Len(TxtString)
            If en = Len(TxtString) Or (Mid(TxtString, en, 1) = " " And en > sn) Then
                ThisVal = 1 + Trim(Mid(TxtString, sn, en - sn))
                sn = en
                If MaxVal = "" Or MaxVal < ThisVal Then MaxVal = ThisVal
            End If
        Next en
        MaxVal = MaxVal - 1
        MaxValue = MaxVal
    End If
    End Function

  4. #4
    Registered User
    Join Date
    05-25-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Find largest number in string containing multiple numbers

    Quote Originally Posted by NickyC View Post
    you could try a user-defined function (I'm assuming there are only commas, spaces and numbers in the text string):

    Function MaxValue(rRange As Range) As Variant
    Dim MaxVal As Variant, ThisVal As Variant, TxtString As String, sn As Variant, en As Variant
    sn = 1 'start character
    MaxVal = "" 'maximum value
    If rRange.Value <> "" And rRange.Value <> " " Then
        TxtString = Replace(rRange.Value, ",", "")
        TxtString = Trim(TxtString)
        For en = 1 To Len(TxtString)
            If en = Len(TxtString) Or (Mid(TxtString, en, 1) = " " And en > sn) Then
                ThisVal = 1 + Trim(Mid(TxtString, sn, en - sn))
                sn = en
                If MaxVal = "" Or MaxVal < ThisVal Then MaxVal = ThisVal
            End If
        Next en
        MaxVal = MaxVal - 1
        MaxValue = MaxVal
    End If
    End Function
    Awesome, that seems to be just what I need, thanks so much!

  5. #5
    Registered User
    Join Date
    05-25-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Find largest number in string containing multiple numbers

    Also just one more thing, how would I find the xth number in the list? e.g. 9nth number listed.
    Would be easy if they all had the same number of decimal places..

  6. #6
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Find largest number in string containing multiple numbers

    As long as the numbers are each separated by a comma and a space...
    A1: 35.328, 32.086, 33.326, 34.479, 34.268, 33.953, 32.497, 31.37, 30.963, 29.252, 29.152, 28.655
    B1: n-th largest value to find....3

    This regular formula will return the n-th largest value in the text.
    EDITED: REPLACE MY ORIGINAL FORMULA (WHICH DID NOT FIND THE LAST NUMERIC VALUE...WITH THIS TWEAKED (AND EQUALLY UGLY) FORMULA
    C1: =LARGE(INDEX(--MID(A$1,FIND("|",SUBSTITUTE(","&A$1&",",",","|",ROW(INDIRECT("1:"&LEN(A$1)-LEN(SUBSTITUTE(A$1,",",""))+1)))),
    FIND("|",SUBSTITUTE(","&A$1&",",",","|",ROW(INDIRECT("2:"&LEN(A$1)-LEN(SUBSTITUTE(A$1,",",""))+2))))
    -FIND("|",SUBSTITUTE(","&A$1&",",",","|",ROW(INDIRECT("1:"&LEN(A$1)-LEN(SUBSTITUTE(A$1,",",""))+1))))-1),0),B1)
    (Yeah...I know....ugly as a mud fence, but it works )

    In the above example, that formula returns: 34.268 (the 3rd largest value)

    If B1: 1
    The returned value is: 35.328 (the largest value)

    Is that something you can work with?
    Last edited by Ron Coderre; 01-18-2012 at 09:17 PM.
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  7. #7
    Forum Guru
    Join Date
    05-24-2011
    Location
    India
    MS-Off Ver
    365
    Posts
    2,243

    Re: Find largest number in string containing multiple numbers

    Try this,

    For MAX, Confirmed with CTRL+SHIFT+ENTER

    =MAX(IFERROR(0+TRIM(MID(SUBSTITUTE(","&A1,",",REPT(" ",LEN(A1))),(ROW(A$1:A$200)*LEN(A1))-1,LEN(A1))),0))

    For xth number

    =INDEX(0+TRIM(MID(SUBSTITUTE(","&A1,",",REPT(" ",LEN(A1))),(ROW(A$1:A$200)*LEN(A1))-1,LEN(A1))),x)

    Change x to a number

    EDIT:

    for the nth LARGEST

    =LARGE(IFERROR(--TRIM(MID(SUBSTITUTE(","&A1,",",REPT(" ",LEN(A1))),(ROW(A$1:A$200)*LEN(A1))-1,LEN(A1))),0),n)


    Array Entered.
    Last edited by Haseeb Avarakkan; 01-18-2012 at 09:12 PM.
    Regards,
    Haseeb Avarakkan

    __________________________________
    "Feedback is the breakfast of champions"

  8. #8
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Find largest number in string containing multiple numbers

    OK...Here's what's depressing...I've got that approach in my formula stash, but never even checked!
    (Really...why do I bother having a stash if I don't even use it)

    Anyway...With
    A1: 35.328, 32.086, 33.326, 34.479, 34.268, 33.953, 32.497, 31.37, 30.963, 29.252, 29.152, 28.655
    B1: the n-th largest value to find

    Using the variation I've got stashed, here's a regular (non-array) version:
    C1: =IFERROR(LARGE(INDEX(--TRIM(MID(SUBSTITUTE(","&$A$1,",",REPT(" ",LEN($A$1))),LEN(A$1)*ROW(INDIRECT("1:"&LEN(A$1)-LEN(SUBSTITUTE(A$1,",",""))+1))
    ,LEN(A$1))),0),B1),"n/a")

  9. #9
    Registered User
    Join Date
    05-25-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Find largest number in string containing multiple numbers

    Quote Originally Posted by Haseeb A View Post
    Try this,


    For xth number

    =INDEX(0+TRIM(MID(SUBSTITUTE(","&A1,",",REPT(" ",LEN(A1))),(ROW(A$1:A$200)*LEN(A1))-1,LEN(A1))),x)
    Thanks I think this mgiht work, but what is the ROW(A$1:A$200) meant to reference?

  10. #10
    Registered User
    Join Date
    05-25-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Find largest number in string containing multiple numbers

    Thanks for the efforts guys, but I just meant nth number chronologically, (not ascending or descending but simply listed from left to right)!
    Cheers

  11. #11
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Find largest number in string containing multiple numbers

    Quote Originally Posted by Snowtoad View Post
    Thanks for the efforts guys, but I just meant nth number chronologically, (not ascending or descending but simply listed from left to right)!
    Cheers
    HAH! That's funny...
    Here you go:
    With
    A1: 35.328, 32.086, 33.326, 34.479, 34.268, 33.953, 32.497, 31.37, 30.963, 29.252, 29.152, 28.655
    B1: the n-th value to find...3

    This regular formula returns that value:
    C1: =IFERROR(--TRIM(MID(SUBSTITUTE(","&$A$1,",",REPT(" ",LEN($A$1))),LEN(A$1)*B1,LEN(A$1))),"n/a")
    In that example, the formula returns: 33.326 (the 3rd value)

    Does that help?
    Last edited by Ron Coderre; 01-18-2012 at 09:49 PM.

  12. #12
    Registered User
    Join Date
    05-25-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Find largest number in string containing multiple numbers

    Yep, that works, thanks so much!

  13. #13
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Find largest number in string containing multiple numbers

    Your title suggests Max not Nth.

    VBA UDFs could be
    Function GetMax(rng As Range)
        Dim arrNos As Variant
        Dim n As Long, TempMax As Double
        
        arrNos = Split(rng, ",")
        For n = 0 To UBound(arrNos)
            If arrNos(n) > TempMax Then TempMax = arrNos(n)
        Next
        GetMax = TempMax
    End Function
    Enter in Excel
    =getmax(A1)

    Function GetNth(rng As Range, nth As Long)
        Dim arrNos As Variant
        
        arrNos = Split(rng, ",")
        GetNth = arrNos(nth - 1) * 1
    End Function
    Enter in Excel
    =GetNth(A1,3)
    For reference only, best to use native formula when ever possible.
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  14. #14
    Registered User
    Join Date
    05-25-2010
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Find largest number in string containing multiple numbers

    Yeah first it was max, NickyC found a solution, then Haseeb and Ron found a solution for the Nth, thanks!

  15. #15
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Find largest number in string containing multiple numbers

    I think you will find that my function "GetMax" for max is a tad more efficient.

    If you have blank rows use like so
    =IF(A1="","",GetMax(A1))
    Last edited by Marcol; 01-19-2012 at 06:47 AM.

  16. #16
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Find largest number in string containing multiple numbers

    Marcol - I agree
    I have never used Split before, but I'm sure I will find it handy in future - thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1