Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Finding the Smallest Even Value.

Finding the Smallest Even Value

Written by Allen Wyatt (last updated November 5, 2020)
This tip applies to Excel 97, 2000, 2002, and 2003


2

Emin has a range of cells in which there can be either text or numbers. He needs a way to determine the smallest even number in the range. Emin wonders if this can be done with a formula, or if he needs a user-defined function.

There are a couple of ways you can approach this problem. One method you can try is to use the DMIN function. All that you need is to make sure that you have a header on your data column (such as "MyData") and then create a small criteria field in some out-of-the-way place. For instance, you might want to create the criteria field by placing a header (such as "Min Even") in cell F1 and place the formula =ISEVEN(MyData) in cell F2. Cell F2 evaluates to an #VALUE! error, but that is fine in this case. You can then use the following formula in a different cell:

=DMIN(A1:A100, 1, F1:F2)

If you prefer, you can use an array formula to figure out the lowest even value. Because your data range can contain text as well as numbers, not all array formulas will work, however. For instance, the following will generate an error if there is anything but numbers in the data range:

=MIN(IF(MOD(A1:A100,2)=0,A1:A100))

To make sure you don't get the errors, you need to do some checking in the formula:

=MIN(IF(ISNUMBER(A1:A100),IF(NOT(MOD(A1:A100,2)=0),"",A1:A100)))

Again, remember that this is an array formula, so you need to enter it using Shift+Ctrl+Enter.

If you prefer, you can create a user-defined function that will return the desired value:

Function MinEven(rng As Range)
    Dim rCell As Range
    Dim bNotFound As Boolean

    Application.Volatile
    MinEven = 9.99 * 10 ^ 307
    bNotFound = True
    For Each rCell In rng
        If Application.WorksheetFunction.IsNumber(rCell) Then
            If rCell Mod 2 = 0 Then
                If rCell < MinEven Then
                    MinEven = rCell
                    bNotFound = False
                End If
            End If
        End If
    Next
    If bNotFound Then MinEven = CVErr(xlErrNum)
End Function

To use this macro, simply use the following with a cell of your worksheet:

=MinEven(A1:A100)

If there are no even numbers in the range, the function will return a #Num error.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (119) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Finding the Smallest Even Value.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Reversing Type

Most text appears black on white, not white on black. If you want to change this so that your type is reversed, here's a ...

Discover More

Turning Off Capital Corrections

If you type two capital letters at the beginning of a word, Word assumes that you made a typing error and will attempt to ...

Discover More

Seeing All Open Workbook Names

Ever want to see a list of all the workbooks that are open? If you open more than nine, Excel only displays the first ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (menu)

Finding the Address of the Lowest Value in a Range

Uncovering the lowest value in a range is relatively easy; you can just use the MIN worksheet function. Discovering the ...

Discover More

Evaluating Formulas

Need a bit of help in figuring out how Excel is evaluating a particular formula? It's easy to figure out if you use the ...

Discover More

Relative References within Named Ranges

Excel is usually more flexible in what you can reference in formulas than is immediately apparent. This tip examines some ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 5 - 3?

2015-12-26 10:36:48

Rick Rothstein

I have simplified the code I posted earlier today by removing one of the Replace function calls (by embedding the number it was replacing directly into the Evaluate function call as a number. Here is my newly modified one-liner UDF for the MinEven function...

Function MinEven(Rng As Range) As Long
MinEven = Evaluate(Replace("MIN(IF(ISNUMBER(@),IF(MOD(@,2)=0,@,9E307),9E307))", "@", Rng.Address))
End Function


2015-12-26 06:01:44

Rick Rothstein

While it is somewhat long, the UDF for MinEven can be written as a one-liner...

Function MinEven(Rng As Range) As Long
MinEven = Evaluate(Replace(Replace("MIN(IF(ISNUMBER(@),IF(MOD(@,2)=0,@,#),#))", "@", Rng.Address), "#", 9 * 10 ^ 307))
End Function


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.