+ Reply to Thread
Results 1 to 10 of 10

Max Func returns Variable name

Hybrid View

  1. #1
    Registered User
    Join Date
    10-01-2009
    Location
    Manassas, VA
    MS-Off Ver
    Excel 2007
    Posts
    86

    Max Func returns Variable name

    This sub below should return a value of 5, how do get this to return variable Five instead?

    Sub MaxVar()
        Dim One, Two, Three, Four, Five As Integer
        One = 1
        Two = 2
        Three = 3
        Four = 4
        Five = 5
    
        MsgBox = WorksheetFunction.Max(One, Two, Three, Four, Five)
    End Sub
    Please advice!

    Thank you,

    NTB
    Last edited by NTB; 09-07-2011 at 09:00 AM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,266

    Re: Max Func returns Variable name

    Hi NTB

    Have you looked at the Choose function? No VBA needed. If you really want to put it in VBA you can use Application.WorksheetFunction.Choose...
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    10-01-2009
    Location
    Manassas, VA
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Max Func returns Variable name

    MarvinP

    Choose, I have to pick the item specifically...

    I had tried, but the function below return Four... unless I have to put everything in order which in real life the values don't line up like that.

    Any other way that you know?


    Sub MaxVar()
        Dim One, Two, Three, Four, Five As Integer
        Dim MaxValue, MaxValueName As String
        One = 1
        Two = 2
        Three = 3
        Four = 4
        Five = 5
        MaxValue = WorksheetFunction.Max(Five, One, Two, Three, Four)
        MaxValueName = Choose(MaxValue, "Five", "One", "Two", "Three", "Four")
        MsgBox MaxValueName
    End Sub

  4. #4
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Max Func returns Variable name

    It might help if you explained a little more about what you're trying to do and why - having Excel return a variable name rather than the value in the variable is quite an unusual thing to do.

  5. #5
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Max Func returns Variable name

    hi, NTB, try this out:

    Sub MaxVar()
        Dim One, Two, Three, Four, Five As Integer
        One = 1
        Two = 2
        Three = 3
        Four = 4
        Five = 5
    
        MsgBox WorksheetFunction.Max(One, Two, Three, Four, Five)
    End Sub
    returns value 5
    Last edited by watersev; 09-07-2011 at 02:48 AM.

  6. #6
    Registered User
    Join Date
    10-01-2009
    Location
    Manassas, VA
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Max Func returns Variable name

    Andrew-R:
    Basically, what I posted was a simplified version. I didn't want to confuse everyone too much.
    My function has 8 different variables, 8 different possible directions: Up, Down, Left, Right, UL, UR, DL, DR

    Each variable would hold a dynamic value for each loop and I would ultimately want to pick the variable with the highest value.

    Instead of doing so many if statements, I thought of Max fuction, but Max only returns the highest value but not the variable that holds it.

    Up = 2
    Down = 0
    Left = 2
    Right = 4
    UL = 4
    UR = 6
    DL = 2
    DR = 4
    To pick DR I would have to do 8 different ifs

    if Up > than the rest then Up
    if Down > the rest the down
    etc etc...

    I would love to have something similiar to

    Dim MaxVar as String
    MaxVar = WorksheetFunction.Max(Up, Down, Left, Right, UL, UR, DL, DL)
    MaxVar would return "UR" because it holds 6, the highest value
    Last edited by NTB; 09-07-2011 at 07:33 AM.

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,266

    Re: Max Func returns Variable name

    Have you looked at the SELECT CASE statement in VBA. It was created for replacing multiple IF statements.
    http://www.ozgrid.com/VBA/select-case.htm or
    http://www.databison.com/index.php/v...ment-in-excel/

  8. #8
    Registered User
    Join Date
    10-01-2009
    Location
    Manassas, VA
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Max Func returns Variable name

    Maybe I'm not understanding this right... but I think it's not relevant to use that here because I would have to compare each variable against the rest.

  9. #9
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Max Func returns Variable name

    OK, how about

    Sub ReturnLargest()
    Dim avVariableValue As Variant
    Dim avVariableName As Variant
    Dim lLargest As Long
    
    avVariableName = Array("Up", "Down", "Left", "Right", "UL", "UR", "DL", "DR")
    
    'Use what-ever code you want to set these values
    avVariableValue = Array(1, 2, 3, 4, 5, 6, 7, 8)
    
    lLargest = WorksheetFunction.Match(WorksheetFunction.Max(avVariableValue), avVariableValue, 0)
    MsgBox avVariableName(lLargest - (1 - LBound(avVariableName)))
    
    End Sub

  10. #10
    Registered User
    Join Date
    10-01-2009
    Location
    Manassas, VA
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Max Func returns Variable name

    Test run
    Sub ReturnLargest()
    Dim avVariableValue As Variant
    Dim avVariableName As Variant
    Dim lLargest As Long
    Dim Up, Down, Left, Right, UL, UR, DL, DR As Integer
    
    Up = 2
    Down = 0
    Left = 2
    Right = 4
    UL = 4
    UR = 6
    DL = 2
    DR = 4
    
    avVariableName = Array("Up", "Down", "Left", "Right", "UL", "UR", "DL", "DR")
    avVariableValue = Array(Up, Down, Left, Right, UL, UR, DL, DR)
    
    lLargest = WorksheetFunction.Match(WorksheetFunction.Max(avVariableValue), avVariableValue, 0)
    MsgBox avVariableName(lLargest - (1 - LBound(avVariableName)))
    End Sub
    Messagebox returns "UR" - DING DING DING.

    Thank you very Much! +1

+ 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