Controlling Automatic Formatting of Dates

Written by Allen Wyatt (last updated November 18, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


7

When Ted enters a date with just the month and day, i.e. 11/5, the date displays 5-Nov and the number format is Custom. However, when he enters 11/5/23 the date displays 11/5/2023 and the number format is Date. Ted wonders how he can get the date to format to 11/5/2023 when he enters only 11/5 without having to format the cells to Date before making his entry. In other words, he wants the date to default to Date and not Custom.

There is no way to do this within Excel. In this case, Excel, as part of its parsing process, chooses the date format that it believes you intended based upon how you entered the date. If you want to use a different date format, you must explicitly format the column (or cells) to use the date format you want before you enter the date, or you must format the column (or cells) explicitly after you enter the date.

There is one macro-based solution, but it may be a bit of overkill. All you need to do is to add this event handler to the code window for the worksheet. (Right-click on the worksheet tab and choose View Code from the resulting Context menu. Paste this short macro into the code window that then appears.)

Private Sub Worksheet_Change(ByVal Target As Range)
    If IsDate(Target) Then
        Target.NumberFormat = "m/d/yyyy"
    End If
End Sub

The macro checks whatever you enter into a cell, and if it is determined to be a date (that's the purpose of the IsDate function), then the desired date format is applied to the cell.

I mentioned that this may be a bit of overkill because you will, obviously, need to save the workbook as a macro-enabled workbook. If this is no problem for your use of the workbook, however, then it may solve your date-formatting needs.

Honestly, the easiest way to handle this is the way that Ted stipulated he didn't want to do—simply format the cells to the Date format before you start doing your entries.

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 (13553) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

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

Protecting an Entire Workbook

Want to stop other people from making unauthorized changes to your workbook? Excel provides a way that you can protect ...

Discover More

ExcelTips: Powerful Lookup Functions (Table of Contents)

Want to access your data indirectly? The answer is to use Excel's lookup function. ExcelTips: Powerful Lookup ...

Discover More

Quickly Deleting Rows and Columns

Deleting rows or columns is easy when you use the shortcut described in this tip. Just select the rows or columns and ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Determining Font Formatting

If you need to determine the font applied to a particular cell, you’ll need to use a macro. This tip presents several ...

Discover More

Ensuring Conditional Formatting and Data Validation is Copied

If you use an Excel worksheet for entering data (a quite common task, actually), then you need to be concerned with how ...

Discover More

Determining If a Cell is Bold

Want to figure out if the text in a cell is bold? The best approach is to use your own user-defined function, as ...

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?

2023-11-21 23:21:05

Tomek

@Joe Fuller:
Thank you for your explanation, I can see why you would want to do this in such specific situation.
For just macros, I organize them in Personal.xlsb by grouping related macros in different modules, appropriately named, so I can easily find all related macros. But as you, I have my style of working with Excel that wouldn't suit everyone. For very specific workbooks, I keep macros and other information in them, but use those as templates rather than what you propose. That helps me, for example, keep my chemical process balances not interfering with my financial spreadsheets, or Data for Navigation Charts updates.


2023-11-21 23:02:51

Tomek

@J. Woolley:
Nice, albeit advanced, trick on triggering a procedure stored in Personal Macro Book. Another way to achieve this is to have an event-triggered procedure for a particular sheet that would call the Personal-Macro procedure; that way you would have full control of which sheets would call that procedure, rather than have every sheet executing it.
My question to @Joe Fuller was to find if there are any benefits from having one more workbook with macros rather than just keeping them in Personal.xlsb and he did answer that question.
My procedure in the second comment was not event-triggered and could be workbook specific, or general kept in Personal.xlsb.


2023-11-19 10:53:31

J. Woolley

@Joe Fuller and Tomek
Sorry guys, but an event procedure like the Tip's Worksheet_Change (or even Workbook_SheetChange) only applies to the worksheet (or workbook) that owns the code; therefore, it doesn't help to put it in another open workbook or in PERSONAL.xlsb.
But here is a way to create an event procedure that DOES apply to external workbooks. The following assumes PERSONAL.xlsb, but it also works with Joe Fuller's always open workbook or with an add-in like MyToolbox.xlam:
1. Press Alt+F11 to open Visual Basic Editor (VBE).
2. Select PERSONAL.xlsb's ThisWorkbook module and press F7 to View Code.
3. Copy and Paste the following code at the beginning of ThisWorkbook:

Private WithEvents MyApp As Application
Private Sub Workbook_Open()
    Set MyApp = Application
End Sub
Private Sub MyApp_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If IsDate(Target) Then
        Target.NumberFormat = "m""/""d""/""yyyy"
    End If
End Sub

4. Press Ctrl+S to save the code before leaving VBE.
Notice NumberFormat is "m""/""d""/""yyyy" to avoid automatic conversion to Short Date as explained by Tomek.
The MyApp_SheetChange event procedure will run every time a cell is changed on any open workbook. This is like swatting flies with a sledge-hammer. Tomek's macro might be more appropriate. Or as suggested by the Tip, "simply format the cells to the Date format" you prefer.
See http://www.cpearson.com/Excel/AppEvent.aspx


2023-11-19 05:50:19

Joe Fuller

Tomek: In answer to your question "How is it different from having the macros in your Personal Macro File PERSONAL.xlsb, which is also always opened when you start Excel?", it probably isn't for anyone that simply wants to call up a macro or several macros, but I use that particular .xlsm file for several things other than for just macros. For me, it's also handy having it always readily available as an extra tab (I use the Office Tab add in) for reference material and also quickly adding / changing macros. I take your point though: how I use that file is not an average user case by any stretch.


2023-11-18 13:15:42

Tomek

Alternatively you can have a similar macro assigned to a keyboard shortcut. For example:

Sub DateEntered()

' Keyboard Shortcut: Ctrl+Shift+D

Selection.NumberFormat = "mm/dd/yyyy"
End Sub

That would enable you to use this shortcut on a cell or range of cells into which you are going to enter the date(s), but also format all selected cells with dates already netered to "correct" their date format. This allows you to enter dates in other cells and leave them in different format, unlike the Change event macro from the tip, which will convert any entered date.

On a tangential note: on my computer (region is Canada), the default date separator is a hyphen, so all "/" in the format will be automatically changed into "-". In such case, and to have full control of the separator, I must enter the format as "MM""/""dd""/""yyyy".
Also, the format "m/d/yyyy" is interpreted as short date and then the date entered on my system is displayed as 2023-11-05 (which is the default short date) rather then the requested 11/5/2023.


2023-11-18 12:44:21

Tomek

Joe Fuller: How is it different from having the macros in your Personal Macro File PERSONAL.xlsb, which is also always opened when you start Excel?


2023-11-18 05:11:31

Joe Fuller

I'm unsure if the following will be of any assistance to Ted, but I thought I'd share how I use all my macros with around 50 different xlsx or csv worksheets I work on each day.

I have well over 200 macros many of which are set up as macro icons on customised tabs as well as the Quick Access Toolbar. Those macros are all saved in one xlsm file, which is always open in Excel. As the macro icons are set up based on that particular xlsm file, even I forget to open it and click on a macro icon, Excel will ask if I want to enable that xlsm file.

So, in short, by having that xlsm file open when working on any other file, those 200 plus macros are always available to me.


This Site

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.

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.