Preventing Automatic Date Formatting Changes

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


5

In a worksheet, Carol has a cell formatted to "Accounting." If someone accidentally enters a date (mm/dd/yy) in that cell, Excel automatically changes the formatting of the cell to show the date correctly. However, if she tries to enter a dollar amount in that cell again, it will not go back to the "Accounting" format; the cell remains in the date format. This is fine if the user sees the error and corrects it, but many times this is happening in a template with "boilerplate" text and the template is protected with no access to formatting cells. Carol wonders if anyone has any ideas on why this happens and how to correct it.

If you want a quick way to prevent the formatting change in simple worksheets, follow these steps:

  1. Display the Excel Options dialog box. (In Excel 2007 click the Office button and then click Excel Options. In Excel 2010 or a later version display the File tab of the ribbon and then click Options.)
  2. At the left side of the dialog box click Advanced.
  3. Scroll to the very bottom of the advanced options. (See Figure 1.)
  4. Figure 1. The Advanced options in the Excel Options dialog box.

  5. Make sure the Transition Formula Entry option is selected.
  6. Click OK.

This particular option causes Excel to evaluate (parse) entered information in the same way that Lotus 1-2-3 did. This means that dates are no longer parsed as dates, but as a formula. Thus, if someone enters 11-16-13 into a cell, then it is parsed as "eleven minus sixteen minus thirteen" and shown in the cell as -18. Because it was not parsed as a date, then the Accounting format is left associated with the cell, as desired.

There are drawbacks to this approach, though. Because Excel subsequently parses any entries according to Lotus rules, your users may conclude that your worksheet doesn't work properly since it doesn't follow the same rules that other Excel worksheets do. This is why I mentioned that this approach may be acceptable for simple worksheets; you'll need to make the determination if your worksheet qualifies.

If you don't want to change how parsing is done, the best approach may be to add some event handlers to your worksheet. For instance, you could include an event handler that looks at where data was entered and ensures that any changes to those cells retain the desired formatting.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngToCheck As Range
    Set rngToCheck = Range("E2")
    If Intersect(Target, rngToCheck) Then
        rngToCheck.NumberFormat = _
          "_($* #,##0.00_);_($* (#,##0.00);_($* "" - ""??_);_(@_)"
    End If
End Sub

In this example, the cell that you want to retain the Accounting format in is E2, as assigned to the rngToCheck variable. If you want to force the format on a different cell range, then just change the assignment line.

If you wanted a bit more flexibility, then you could use a different set of event handlers. For instance, the following examples use both the SelectionChange and Change events of the Worksheet object. They result in something that doesn't so much force a particular format, but it prevents the formatting of a cell from being changed from whatever it was before. Thus, this approach protects any formatting, not just enforcing an Accounting format.

Dim nFormat As String

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngToCheck As Range
    Set rngToCheck = Range("E2")
    If Intersect(Target, rngToCheck) Then
        rngToCheck.NumberFormat = nFormat
    End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    nFormat = Target.NumberFormat
End Sub

The SelectionChange event handler fires first, setting the existing format into the nFormat variable. Then the Change event handler fires and sets the formatting back to the original.

Another approach you could try is to use data validation. This approach doesn't require any macros and is therefore suitable if your workbook will be used by people who may have macros disabled on their system. Follow these general steps:

  1. Select the cell or cells whose formatting you want to enforce.
  2. Display the Data tab of the ribbon.
  3. Click the Data Validation tool in the Data Tools group. Excel displays the Data Validation dialog box.
  4. Using the Allow drop-down list, choose Custom. (See Figure 2.)
  5. Figure 2. The Data Validation dialog box.

  6. In the Formula box, type the following: =CELL("format",B2)="C2"
  7. Set any other data validation settings as desired.
  8. Click OK.

The formula (step 5) checks the formatting of the cell and either allows or disallows entry based on that formatting. In the formula as cited, format C2 is the internal name for the Accounting format. You could easily change the codes in the formula to some other format, such as ",2", "C2", "C0", "C2-", or "C0-" depending on your preference. The easiest way to figure out what format you should use is to format a cell, as desired, before applying the data validation rule. (For instance, let's say you apply the formatting to cell L13.) You can then use this formula in a different cell to see what format Excel believes you've applied:

=CELL("format",L13)

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

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

Creating a Table of Authorities

In legal documents a table of authorities is a common element. Creating the table is easy to do if you apply the ...

Discover More

Opening a Backup File

If you have Word configured to save backup copies of your document, you may want to actually load one of those copies at ...

Discover More

Hanging Indent Shortcut

You can use the tools on the ribbon to adjust the indent applied to a paragraph. If you want to format a hanging indent, ...

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 (ribbon)

Creating a Center Across Selection Button

The ability to center text across a range of cells has long been a staple of experienced Excel users. Here's how to ...

Discover More

Merge and Center Not Available

What are you to do if you are trying to format a worksheet, only to find out that one of the tools you need is not ...

Discover More

Automatically Copying Formatting

It's easy to automatically set the contents of one cell to be equal to another cell. But what if you want to copy the ...

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?

2023-08-04 12:46:12

Wayne

This does not correct the issue for me. When I enter a range (e.g. "10-15") in any cell, the result is "15-Oct". The format is not changed to Date but rather to Custom. The Advance Option edit described above has no effect. If I enter a range as indicated above. I must enter a leading single quote to force Excel to treat the entry as nothing more than general characters with no deeper meaning.


2022-01-07 06:04:43

John

Such an annoying problem, such a straightforward fix! Thanks!


2020-02-07 01:31:12

yudi

thanks its very helpful!


2019-03-23 19:30:45

John Mann

If I understand the tip correctly, the formula :"=CELL("format",L13)" is used to find out the "formating code" for a particular formula. Thus if you format cell L13 as Percentagbe, the execuate that formula, you will get back a code P2, If you set it to either Accounting or Currency you get back C2 (at least in Excel 2010). This is intended as a test to find out what code to enter in the data validation formula "=CELL("format",B2)="C2"" if you want to protect some fromat other than the acoounting format requested in the Help Wanted.

My testing seems to imply that it won't distinguish between Accounting format and Currency format, though I haven't tested the actual data validation process.


2019-03-23 11:55:04

Brian Primeau

How does Excel know what format "C2" consists of? That is, how do you tell Excel what the format is that you want to enforce in L13?


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.