Preventing Automatic Date Formatting Changes

by Allen Wyatt
(last updated January 11, 2019)

2

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 Office 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

Rounding to the Nearest Half Dollar

When working with financial data, it's easy to round values to the nearest dollar. What if you want them rounded to the ...

Discover More

Consolidating Changes under a Single User

When using Track Changes, you may want to have your edits appear to be those of a different user. Here's how to fool Word ...

Discover More

Table Numbers are Skipped

What do you do if you add numbered captions to an element of your document (such as tables) and Word skips a number? ...

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

Adjusting Cell Margins for More White Space

Is the information in your cells too jammed up? Here are some ways you can add some white space around that information ...

Discover More

Filling a Cell

One way you can format a cell is so that its contents are repeated over and over again for the entire width of the cell. ...

Discover More

Setting Orientation of Cell Values

Need the contents of a cell to be shown in a direction different than normal? Excel makes it easy to have your content ...

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}] 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 4 - 0?

2013-11-18 11:02:25

Carol

Thanks for the help! I think the data validation will work best for what I am doing.

Vicky, I think data validation will also work for %. Just figure out what the internal name for the percentage format you are using - P2 (2 decimal places), P1 (1 decimal place) etc.

Thanks again. Great tips!


2013-11-16 07:11:48

vicky

what about % formating. % also does not get removed. provide solution.


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.