Welcome toExcel.Tips.Net
Tips.Net Home
ExcelTips Home
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Learn Access Now
Free Printable Forms
Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site
Filtering Columns for Unique Values
Printing Multiple Worksheets on a Single Page
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
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (7197) applies to Microsoft Excel versions: 97 2000 2002 2003 2007
More Power! For some people, the prospect of creating macros can be scary. Those who conquer their fears, however, find they become much more confident and productive once they learn how to make Excel do exactly what they want. ExcelTips: The Macros is an invaluable source for learning Excel macros. You are introduced to the topic in bite-sized chunks, pulled from past issues of ExcelTips. Learn at your own pace, exactly the way you want.