Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Finding the Address of the Lowest Value in a Range.
by Allen Wyatt
(last updated September 15, 2018)
When writing a macro, you can find the lowest value in a range of cells by using the WorksheetFunction method to apply the MIN worksheet function. You may need, however, to not only find the lowest value in the range, but also the address of the first cell that contains that value.
One simple way is to simply step through the range you want to examine and derive both the lowest value and the address of the cell being examined, as in the following:
Function FindLowestAddr(pRng As Range) As String Dim MinVal As Double Dim MinAddr As String Dim c As Range MinVal = pRng.Cells(1).Value MinAddr = pRng.Cells(1).Address For Each c in pRng If c.Value < MinVal Then MinVal = c.Value MinAddr = c.Address End If Next c FindLowestAddr = MinAddr End Function
Note that this approach doesn't rely upon the MIN worksheet function at all. There is a drawback to it, however—it doesn't differentiate between cells that contain numeric values and those that don't. In other words, if the range passed to the function contains a blank cell, that cell is considered to contain a zero value, which may very well be the lowest value in the range.
One way around this is to rely upon worksheet functions from within the macro. The following macro uses both the MIN and MATCH worksheet functions to determine the location of the minimum value and then the index (offset) of that cell within the range.
Function GetAddr(rng As Range) As String Dim dMin As Double Dim lIndex As Long Dim sAddress As String With Application.WorksheetFunction dMin = .Min(rng) lIndex = .Match(dMin, rng, 0) End With GetAddr = rng.Cells(lIndex).Address End Function
It should be noted that if you are using the macro only to discover the address because you figured there was no way to derive the desired information without the macro, then you can do away with the macro entirely by using a worksheet formula. For instance, if you want to determine the address of the lowest-valued cell in the named range MyRange, you could use the following:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12744) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Finding the Address of the Lowest Value in a Range.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
It is not unusual to use worksheets to collect information over time. As you keep adding information to the worksheet, ...Discover More
Formulas are made up of operands that separate a series of terms acted upon by the operands. You may want to know, for ...Discover More
Excel is used by many people to hold all sorts of data, not just numbers. If you have cells that include meaningful ...Discover More
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.