Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find the maximum value in a range with VBA
#1
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.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Format cells in a range with bold font and yellow background color Qomplainerz 0 772 07-27-2023, 11:26 AM
Last Post: Qomplainerz
  Find a specific value in a range and replace it with a new value Qomplainerz 0 385 07-27-2023, 10:38 AM
Last Post: Qomplainerz
  Sort data in a range with VBA Qomplainerz 0 367 07-27-2023, 09:43 AM
Last Post: Qomplainerz
  Range methods in VBA Qomplainerz 0 304 07-27-2023, 06:29 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 10 Guest(s)