Adjusting Date Values by Keypress

Written by Allen Wyatt (last updated January 21, 2023)
This tip applies to Excel 97, 2000, 2002, and 2003


Robert notes that in a Quicken date field, if he presses the plus or minus key the date increments or decrements by one day. He would like to create this same sort of effect in Excel.

This is a harder problem to approach than one might assume, particularly in Excel. Since an action needs to be taken upon the pressing of a particular key (in this case, the plus or minus keys), one would naturally assume that the OnKey method could be used. Consider the following examples:

Sub Start_OnKey()
    Application.OnKey "{+}", "Plus1"
    Application.OnKey "-", "Minus1"
End Sub
Sub End_OnKey()
    Application.OnKey "{+}"
    Application.OnKey "-"
End Sub
Sub Plus1()
    If IsDate(ActiveCell) And Not ActiveCell.HasFormula Then
        ActiveCell.Value = ActiveCell.Value + 1
    End If
End Sub
Sub Minus1()
    If IsDate(ActiveCell) And Not ActiveCell.HasFormula Then
        ActiveCell.Value = ActiveCell.Value - 1
    End If
End Sub

According to all the VBA documentation, the above should work just fine, once you run the Start_OnKey macro. Every time a plus or minus key is pressed, the appropriate procedure is run to either increment the date or decrement the date. The problem is, it won't work on some versions of Excel. Why? Because the plus key, when pressed, apparently puts some versions of Excel into a special "formula entry" mode that bypasses the normal keyboard buffer relied upon by OnKey. So while pressing the minus key while a cell containing a date is selected produces the desired result, pressing the plus key does not.

For those versions of Excel where the plus key is a problem, the only solution is to change the keystrokes to something else. For instance, you could change the keypresses so that Ctrl+u is used to increment the date and Ctrl+d is used to decrement the date:

Sub Start_OnKey()
    Application.OnKey "^u", "Plus1"
    Application.OnKey "^d", "Minus1"
End Sub
Sub End_OnKey()
    Application.OnKey "^u"
    Application.OnKey "^d"
End Sub

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 (7197) applies to Microsoft Excel 97, 2000, 2002, and 2003.

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

Wrapping Text Around a Graphic

Place a graphic in your document, and you may want to make sure that your document text "wraps" around the edges of the ...

Discover More

Changing Multiple Cells at Once

Excel includes several different methods of editing information in your cells. If you want to edit multiple cells all at ...

Discover More

Searching for Formatting

When searching for text, Word can pay attention to more than just the characters in the text. It can also pay attention ...

Discover More

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!

More ExcelTips (menu)

Converting UTC Times to Local Times

Dates and times are often standardized on UTC time, which is analogous to GMT times. How to convert such times to your ...

Discover More

Setting a Default Date Format

Enter a date into a cell, and Excel allows you to format that date in a variety of ways. Don't see the date format you ...

Discover More

Displaying Negative Times

Excel allows you to perform math using times as operands. If you subtract a later time from an earlier time, you should ...

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 8 - 5?

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.