Can't Set Custom Format in VBA

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


Stuart uses the NumberFormat property, in a macro, to set the custom format for a cell. He sets it equal to #,##0.00_);[Red](#,##0.00), but when he later looks at it, Excel changes the format to #,##0.00;[Red]-#,##0.00.

You can set the custom format of a selection of cells in this manner:

Sub SetCells()
    Selection.NumberFormat = "#,##0.00_);[Red](#,##0.00)"
End Sub

Similarly, you could set the custom format of a specific range of cells in this manner:

Sub SetCells()
    Range("A1").NumberFormat = "#,##0.00_);[Red](#,##0.00)"
End Sub

Either approach sets the custom format correctly, as confirmed through testing. If the format doesn't appear as expected (and you are sure the format pattern matches what is shown above), then there are only two reasons I can think of that would cause the discrepancy. First, it is possible that other macro code is changing the custom format without your knowledge, particularly code in an event handler. Why an event handler? Because event handlers often make changes automatically, typically after cell changes or even moving from one cell to another.

The second possibility is that what you are seeing in the worksheet is not the result of the custom format, but actually the result of a conditional format. You'll need to check and possibly change the conditional formatting rules to deal with this possibility.

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

AutoText Unavailable in Headers and Footers

When you are creating headers and footers for your document, you might want to use some of your AutoText entries. What if ...

Discover More

Forcing a Worksheet to be Protected Again

Excel allows you to protect your worksheets so they can only be changed as you want to have happen. If you unprotect a ...

Discover More

Printing Shortcut Key Assignments from a Macro

Need to know what shortcut keys are defined? You can use a single macro command line to print out the definitions.

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Notation for Thousands and Millions

When working with very large numbers in a worksheet, you may want the numbers to appear in a shortened notation, with an ...

Discover More

Creating Two-Line Custom Formats

Creating custom formats is a very powerful way to display information exactly as you want it to appear. Most custom ...

Discover More

Using a Custom Format to Add Dashes

Want some dashes automatically added in values you display in a cell? It may be trickier to develop a custom format than ...

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 6 - 0?

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