Consulting

Results 1 to 4 of 4

Thread: VBA to Find Maximum Value in a Range

  1. #1
    VBAX Regular
    Joined
    Feb 2018
    Location
    Portland
    Posts
    38
    Location

    VBA to Find Maximum Value in a Range

    Hello,

    I am typing in the following code and getting an error "run time 1004 unable to find Max". Any ideas? Thanks

    Sub ColorMaximumValue()
        Dim rng As Range, cell As Range
        Set rng = Range("A1:A5")
        Debug.Print Application.WorksheetFunction.Max("rng")
    End Sub
    RangeError.jpg

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Try


    Application.WorksheetFunction.Max(rng)
    
    without the quotes
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Feb 2018
    Location
    Portland
    Posts
    38
    Location
    Thanks Paul, "brain cramp" moment. Forgot that for variable names, you do no need the quotation marks. It worked great

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Can be a little confusing

    If had a range named "rng", then it would be sort of similar

    Sub ColorMaximumValue()
        Dim rng As String
        Range("A1:A5").Name = "rng"
        Debug.Print Application.WorksheetFunction.Max(range("rng"))End Sub

    You can use Tread Tools and mark this one [SOLVED]
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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