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: Limiting Entry of Prior Dates.

Limiting Entry of Prior Dates

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


If you use the data validation capabilities of Excel, you can limit what goes into a cell, based on the contents of another cell. For instance, you can easily limit what goes into cell A2 based on a date that is in cell A1. Follow these steps:

  1. Select cell A2.
  2. Choose Validation from the Data menu. Excel displays the Data Validation dialog box.
  3. Make sure the Settings tab is displayed. (See Figure 1.)
  4. Figure 1. The Settings tab of the Data Validation dialog box.

  5. Using the Allow drop-down list, choose Date.
  6. Using the Data drop-down list, choose Greater Than or Equal To.
  7. In the Start Date box, enter =A1. This tells Excel that the date must be greater than or equal to whatever date is in cell A1.
  8. Click OK.

Now, anytime you try to enter a date in cell A2 that is earlier than the date in cell A1, Excel displays an error message and will not allow the date to be entered.

What happens, however, when you want to limit the dates that can be entered in cell A1? For instance, if you put the date 4/1/04 in cell A1, and you want to make sure that the next date entered in A1 is not earlier than 4/1/04. If you put a date such as 4/15/04 in cell A1, that would be OK, but then the next time you enter a date in cell A1 you don't want it earlier than 4/15/04. In other words, you want to make sure that cell A1 can only accept dates later than the date currently in A1.

This is a bit stickier. If you follow the above steps but select cell A1 in step 1, then data validation won't work. Why? Because the date you enter in cell A1 will always be greater than or equal to the date you enter in A1—Excel doesn't compare to the previous date in A1 when doing data validation.

The only way to work through this problem is through the use of two macros. First, place the following macro in a regular module:

Sub Date_Validation()
    Dim dteDate As Date
    Dim strDate As String

    With Range("A1")
    '   Memo original date
        dteDate = CDate(.Text)
    '   Create date string
        strDate = Format(dteDate, "m\/d\/yy")
        With .Validation
        '   Delete old settings
            .Delete
        '   Set new data validation
            .Add _
              Type:=xlValidateDate, _
              AlertStyle:=xlValidAlertStop, _
              Operator:=xlGreaterEqual, _
              Formula1:=strDate

            .IgnoreBlank = False
            .InCellDropdown = True
            .InputTitle = ""
            .ErrorTitle = "Invalid Date Entry"
            .InputMessage = ""
            .ErrorMessage = _
              "Date is older than the previous date (" & _
              dteDate & ")."
            .ShowInput = True
            .ShowError = True
        End With
    End With
End Sub

This macro needs to be called by another macro, this one placed in the worksheet's code window, so that it is triggered every time there is a change in the worksheet:

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Target = Range("A1") Then Date_Validation
End Sub

The way these macros work is really quite interesting. Because you place the latter one in the worksheet's code window, it triggers every time there is a change to the worksheet. If the cell being changed is A1, then the Date_Validation macro is run.

The Date_Validation macro grabs the date from cell A1 and constructs a data validation rule for the cell. That's all it does—sets a data validation rule that won't allow a date to be entered in the cell that is earlier than the date currently in the cell.

The beauty of the macro is that once the data validation rule is in effect, then the next time cell A1 is changed, the data validation rule is triggered before the Worksheet_Change event is fired. Thus, the data validation rule makes sure that only a date greater than the current date can be entered. Once data validation is cleared, then the macro takes care of resetting the data validation rule, so it compares to the newly entered date.

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 (2960) 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: Limiting Entry of Prior Dates.

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

Ignoring Hyphens in Word Counts

When you instruct Word to tell you how many words are in a document, it treats hyphenated words or phrases as if they are ...

Discover More

Stepping Through a Macro with a Worksheet Visible

When developing a macro, it is often necessary to step through the various code lines so you can see what is happening on ...

Discover More

Ignoring Accented Characters in Searches

When writing in non-English languages, there can be many variations of accented characters that are used in a word. You ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Creating Selections

Want a really easy way to create a selection of a group of cells? Discover how to use the Extend key to make this task ...

Discover More

Errors when Copying References to External Cells

If you copy a cell that contains a reference to external data, do you get an error? It could be due to the complexity of ...

Discover More

Picking a Contiguous Range of Cells

There are a variety of ways to pick a range of cells in Excel. Here are three of them you'll find useful.

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 2 + 2?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.