+ Reply to Thread
Results 1 to 3 of 3

Finding highest number in a range of cells

Hybrid View

  1. #1
    Registered User
    Join Date
    08-02-2007
    Posts
    30

    Finding highest number in a range of cells

    Hi,

    I am working with a "register" in excel. What VBA code should I use to find the highest number in a range of cells (not sorted in particular order)? All numbers are integers.

    I would like to know the actual number in that cell, the cell reference and the row number.


    Thx,


    Marcus

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Like this

    Option Explicit
    
    Sub findMax()
    
        Dim rng    As Range
        Dim cl     As Range
        Dim maxNum As Long
    
        Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
    
        maxNum = Application.WorksheetFunction.Max(rng)
    
        Set cl = rng.Find(maxNum, LookIn:=xlValues)
    
        If Not cl Is Nothing Then
    
            MsgBox " Maximum value: " & maxNum & vbNewLine & "Located in:" & cl.Address & _
                   vbNewLine & "In Row: " & cl.Row
        End If
    
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    08-02-2007
    Posts
    30

    Max(rng) function was spot on!

    Thx Roy,

    this helped perfectly fine.

    Marcus

    Quote Originally Posted by royUK
    Like this

    Option Explicit
    
    Sub findMax()
    
        Dim rng    As Range
        Dim cl     As Range
        Dim maxNum As Long
    
        Set rng = Range(Cells(1, 1), Cells(Rows.Count, 1).End(xlUp))
    
        maxNum = Application.WorksheetFunction.Max(rng)
    
        Set cl = rng.Find(maxNum, LookIn:=xlValues)
    
        If Not cl Is Nothing Then
    
            MsgBox " Maximum value: " & maxNum & vbNewLine & "Located in:" & cl.Address & _
                   vbNewLine & "In Row: " & cl.Row
        End If
    
    End Sub

+ 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