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
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.
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