VBA Macro used in the Microsoft Office Excel Application

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
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Search for Excel Macros

Recent Macros

VBA Macro used in the Microsoft Office Outlook Application

Outlook Greeting and Goodbye Contacts | VBA Macro #35

The Greeting and Goodbye Contact macro automates the entry of a Greeting “Good Morning” or “Hello” followed by the First Name of the recipient you are emailing. To identify the First Name this macro uses the recipient’s email address to loop through all the existing contact records for a matching email address. If a match is found the contact’s First Name field value is used in the beginning of the email. The appropriate Greeting and Goodbye is determined by the current time and the day of the week. The Greeting and Goodbye Contacts macro helps you deliver better customer service and create better connections with your clients!

VBA Macro used in the Microsoft Office Excel Application

Excel Custom Questionnaire to File | VBA Macro #34

This is an Excel macro that generates a custom questionnaire to complete.  The Role field is used to determine what questions display for specific users.  Once a user selects their Role, they can click Generate to see what questions they need to complete to finish the questionnaire.  After a user has completed all the required questions, they can click Finish to create a separate Excel Workbook with all the populated answers.   You can customize where this new file is saved making it easy to know when someone has completed a questionnaire and is awaiting review. There is also the option to send this separate Excel Workbook in an Outlook email with a couple updates to the VBA code. There is always a better way with VBA and this Excel macro helps standardize the collection of answers to specific questions. This can help lead to a more formal review process and eliminate mistakes.

VBA Macro used in the Microsoft Office Outlook Application

Outlook Hello Contacts | VBA Macro #33

Starting an email with a Hello can look more personable with some VBA code! The Hello Contacts Outlook macro automates the entry of the text Hello followed by the First Name of the recipient you are emailing. This Outlook macro uses the recipient’s email address to loop through all the existing contact records for a matching email address. If a match is found the contact’s First Name field value is used in the beginning of the email. This macro can be fired from a custom button or trigger automatically when replying to an email.

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 Excel, Outlook, 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

Looking for more?

Subscribe as an Insider to receive additional rights.

Contact Us

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

Copyright © 2019 Leverage & Lean – “Less Clicks, More Results”