Finding Cell with Minimum/Maximum Value in Active Worksheet

Posted on the October 4th, 2008 under Excel VBA Function, Workbook and Worksheet by Poer @ Excel VBA/Macro

Let say we want to find position of cell containing the minimum/maximum value in current/active Excel worksheet, and then after we found the cell, we will change the cell format to make it stand out before other cells.

The logic is simple, we just need to use Excel MIN function to find the minimum/maximum value on the worksheet, and then using Excel FIND function we will find which cell contain that minimum/ maximum value.

Excel VBA macro implementation of the algorithm above will look like below, change code Application.Min(oRg) into Application.Max(oRg) to find the maximum value instead of minimum value.

Sub FindMinValue()

    Dim oRg As Range, iMin As Variant

    Set oRg = Cells
    'Finding the minimum value
    iMin = Application.Min(oRg)

    'Select cell containing the min value
    oRg.Find(What:=iMin, _
        After:=oRg.Range("A1"), _
        LookIn:=xlValues, _
        LookAt:=xlPart, _
        SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False _
        ).Select

    'Change selected cell format
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent3
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

    'Displaying min value info
    With Selection
        MsgBox "Min value : " & iMin & vbCrLf & _
        "Cell position " & vbCrLf & _
        "Row : " & .Row & vbCrLf & _
        "Column : " & .Column
    End With

End Sub

As an additional info, the code above will display the cell info, row and column position of the cell containing the minimum/maximum value.

Related Entries

External Resources

Tagged with:

6 Responses to 'Finding Cell with Minimum/Maximum Value in Active Worksheet'

  1. December 4, 2008 at 6:53 pm
    Paul
  2. January 29, 2009 at 10:44 am
    Poer
  3. April 2, 2009 at 3:17 pm
    NewToVBA
  4. April 24, 2009 at 4:09 am
    Jenn
  5. April 26, 2009 at 10:21 am
    Poer
  6. October 28, 2009 at 10:16 pm
    M?nimo de un rango de celdas - Ayuda Excel

Leave a Reply




XHTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
SYNTAX HIGHLIGHTER:
Place your VBA code between <pre> tags like this <pre class="brush:vb"> sub vba() ... end sub </pre>.