Find the maximum value in a range with VBA - Printable Version +- QP School (https://qomplainerzschool.lima-city.de) +-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3) +--- Forum: Excel VBA Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=48) +--- Thread: Find the maximum value in a range with VBA (/showthread.php?tid=5241) |
Find the maximum value in a range with VBA - Qomplainerz - 07-27-2023 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. |