QP School

Full Version: Find the maximum value in a range with VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Example:

Sub FindMaxValue()
    Dim rng As Range
    Dim maxVal As Double

    'Set the range where you want to find the maximum value
    Set rng = Range("A1:A10")

    'Find the maximum value in the range
    maxVal = Application.WorksheetFunction.Max(rng)

    'Display the maximum value
    MsgBox "The maximum value in the range is: " & maxVal
End Sub

Explanation:

This code defines a subroutine named FindMaxValue. 
It declares two variables rng (to represent the range) and maxVal (to store the maximum value). 
The Set statement is used to define the range of cells (A1 to A10) where you want to find the maximum value. 
The Max function from the WorksheetFunction object is used to find the maximum value in the range. 
The maximum value is then displayed in a message box.

The Max function is part of the WorksheetFunction object, and it returns the maximum value from a given range of cells. 
By using the Application object to access the WorksheetFunction, we can call the Max function in VBA.