Selecting the Maximum Value in a RangeThis tip describes a simple utility that activates the worksheet cell that contains the maximum value. The VBA routine determines the maximum value in the selected range. If a single cell is selected, it determines the maximum value for the entire worksheet. Next, it use the Find method to locate the value and select the cell. To use this subroutine:
The GoToMax SubroutineSub GoToMax()
' Activates the cell with the largest value
Dim WorkRange as Range
' Exit if a range is not selected
If TypeName(Selection) <> "Range" Then Exit Sub
' If one cell is selected, search entire worksheet;
' Otherwise, search the selected range
If Selection.Count = 1 Then
Set Workrange = Cells
Else
Set Workrange = Selection
End If
' Determine the maximum value
MaxVal = Application.Max(Workrange)
' Find it and select it
On Error Resume Next
Workrange.Find(What:=MaxVal, _
After:=Workrange.Range("A1"), _
LookIn:=xlValues, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False _
).Select
If Err <> 0 Then MsgBox "Max value was not found: " & MaxVal
End Sub
|