Excel Find Largest and Smallest Value | VBA Macro #36

Share on facebook
Share on twitter
Share on linkedin
This is an Excel macro that will find the Largest and Smallest values in a Workbook. By Default, this Excel macro will loop through the entire Active Workbook. Once this Excel macro finishes a Message Box will display the Active Workbook that was searched and the Max and Min values found. It is easy to customize the Excel Find Largest and Smallest Value macro to only search through a specific range of cells for these values. This macro can stand alone or be used as a function within existing macros. With some additional updates to the VBA code you can have this Excel macro fire every time an Excel Workbook opens! No longer do you need to sort or use conditional formatting to identify the Largest and Smallest values in Excel. There is a better way with VBA so start using the Excel Find Largest and Smallest Value today!

Use the following link to download the Excel Workbook seen in the videos below: Fruit Inventory Status

See it in Action!

Watch this video to see this macro in action.

The Code

Here is the code for this macro. Make sure the following References are setup before running it: Visual Basic For Applications, Microsoft Excel 16.0 Object Library
'Leverage & Lean "Less Clicks, More Results"
Sub FindLargestSmallestValue()
' Means that these variables are in use
Dim CurrentMax As Double '
Dim CurrentMin As Double '
Dim LastColumnLetter As String '
Dim LastColumnNumber As Integer '
Dim LastRow As Integer '
Dim Max As Double '
Dim Min As Double '
Dim Rng As Range '
Dim Ws As Integer '

Ws = 1

Do Until Ws > Worksheets.Count 'Loop through every Worksheet]
LastRow = Sheets(Ws).Cells(Rows.Count, 1).End(xlUp).Row 'Identify Last Row
LastColumnNumber = Sheets(Ws).Cells(1, Columns.Count).End(xlToLeft).Column 'Identify Last Column Number
LastColumnLetter = Replace(Sheets(Ws).Cells(1, LastColumnNumber).Address(True, False), "$1", "") 'Identify Last Column Letter
Set Rng = Sheets(Ws).Range("A1:" & LastColumnLetter & LastRow) 'Set Range
CurrentMin = Application.WorksheetFunction.Min(Rng) 'Worksheet Function MIN returns the Smallest value in a Range
CurrentMax = Application.WorksheetFunction.Max(Rng) 'Worksheet Function MAX returns the Largest value in a Range
If CurrentMax > Max Then Max = CurrentMax 'If Current Max found is Larger then Max set
If Min > CurrentMin Then Min = CurrentMin 'If Current Min found is Smaller then Min set
If Ws = 1 Then Min = CurrentMin 'Set Min from first Worksheet so initial 0 isn't returned at finish
Ws = Ws + 1
Loop

MsgBox "This Workbook " & ActiveWorkbook.Name & " has a Max of " & Max & " and Min of " & Min

End Sub
'Stay Awesome!

Macro Monday

Here is the Macro Monday video this macro was featured in. Watch this video to learn how to get the most out of this macro and start using it today!

Customization

These lines of code can be customized to personalize this macro.
20. Set Rng = Sheets(Ws).Range(“A1:” & LastColumnLetter & LastRow) ‘Set RangeDetails to Customize
23. If CurrentMax > Max Then Max = CurrentMax ‘If Current Max found is Larger then Max set Update the Current Max value if a new one is found.
24. If CurrentMin < Min Then Min = CurrentMin 'If Current Min found is Smaller then Min set Update the Current Min value if a new one is found.
25. If Ws = 1 Then Min = CurrentMin ‘Set Min from first Worksheet so initial 0 isn’t returned at finish Set the initial Current Min found on the first instance of the Rng.
29. MsgBox “This Workbook ” & ActiveWorkbook.Name & ” has a Max of ” & Max & ” and Min of ” & Min This is the MsgBox that will display once this macro finishes.

You can also call this Excel macro immediately when a Workbook opened by pasting this code into the ThisWorkbook portion of Visual Basic.
Sub Workbook_Open()

'Call MacroName
Call FindLargestSmallestValue

End Sub

First time using VBA?

The Developer Tab is an additional section of the ribbon when activated allows you access to Visual Basic in Applications like Access, Excel, Outlook, PowerPoint, Word.
Setup Now
Once the Developer Tab is available you will have the capability to update your security to allow Macros to run in the current application.
Setup Now
A sub of code is a collection of objects and variables. For the code to successfully run a library of references needs to be set.
Setup Now
Subscribe as an Insider to receive additional rights.
If you like our content and want to show your support tip us here!

Contact Us

Looking to improve your computer processes?
Leverage & Lean is here to help!

Leverage & Lean

Is focused on improving computer processes by Leveraging programming and applying Lean principles. It is our goal to make you more efficient and effective on your computer producing “Less Clicks and More Results!”