65

Can you please let me know what are the .NumberFormat format options in Excel VBA? As you are fully aware Excel 2010 supports the following types:

enter image description here

I know that we can set for example Text type as:

.NumberFormat ="@"

or for number:

.NumberFormat = "0.00000"

Can you please let me know what are other options for types in VBA?

Teamothy
  • 2,000
  • 3
  • 16
  • 26
user1760110
  • 2,256
  • 5
  • 29
  • 37
  • 6
    Why don't you record a macro and check? – Siddharth Rout Dec 18 '13 at 01:36
  • 4
    In the `Format Cell` dialog, click on the format type you want (eg.,'Accounting`), and then click `Custom`. The list on the bottom right shows the current `Accounting` format string (along with others). The textbox above shows you the `NumberFormat` string for that item. You can copy/paste right from that textbox into a string in your code. – Ken White Dec 18 '13 at 01:45
  • 2
    Here's a somewhat goofy, but colorful, custom format preview web page that I created: http://yoursumbuddy.com/preview-excel-custom-formats/ – Doug Glancy Dec 18 '13 at 03:57

5 Answers5

95

Note this was done on Excel for Mac 2011 but should be same for Windows

Macro:

Sub numberformats()
  Dim rng As Range
  Set rng = Range("A24:A35")
  For Each c In rng
    Debug.Print c.NumberFormat
  Next c
End Sub

Result:

General     General
Number      0
Currency    $#,##0.00;[Red]$#,##0.00
Accounting  _($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)
Date        m/d/yy
Time        [$-F400]h:mm:ss am/pm
Percentage  0.00%
Fraction    # ?/?
Scientific  0.00E+00
Text        @
Special     ;;
Custom      #,##0_);[Red](#,##0)

(I just picked a random entry for custom)

doovers
  • 8,545
  • 10
  • 42
  • 70
  • 4
    The "s of Accounting need to be escaped, `""` to prevent bugs in vba that uses that code. – puzzlepiece87 Aug 25 '16 at 15:33
  • 1
    I need to know what you meant by 'escaped'. Are you saying that we need to double quote the hashmark (-) in order for this to work in VBA? – John Shaw Aug 21 '18 at 22:32
  • 2
    Personally, I prefer & chr(34) & - it's much cleaner – Selkie Feb 04 '19 at 16:17
  • The escaped version used in actual code looks like this: `YourRange.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"`. If you want the cleaner look @Selkie mentioned above it looks like this: `YourRange.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* " & Chr(34) & "-" & Chr(34) & "??_);_(@_)"`. – ChrisB Jan 05 '23 at 06:35
23

Thanks to this question (and answers), I discovered an easy way to get at the exact NumberFormat string for virtually any format that Excel has to offer.


How to Obtain the NumberFormat String for Any Excel Number Format


Step 1: In the user interface, set a cell to the NumberFormat you want to use.

I manually formatted a cell to Chinese (PRC) currency

In my example, I selected the Chinese (PRC) Currency from the options contained in the "Account Numbers Format" combo box.

Step 2: Expand the Number Format dropdown and select "More Number Formats...".

Open the Number Format dropdown

Step 3: In the Number tab, in Category, click "Custom".

Click Custom

The "Sample" section shows the Chinese (PRC) currency formatting that I applied.

The "Type" input box contains the NumberFormat string that you can use programmatically.

So, in this example, the NumberFormat of my Chinese (PRC) Currency cell is as follows:

_ [$¥-804]* #,##0.00_ ;_ [$¥-804]* -#,##0.00_ ;_ [$¥-804]* "-"??_ ;_ @_ 

If you do these steps for each NumberFormat that you desire, then the world is yours.

I hope this helps.

Kevin Lee Garner
  • 857
  • 10
  • 12
9

dovers gives us his great answer and based on it you can try use it like

public static class CellDataFormat
{
        public static string General { get { return "General"; } }
        public static string Number { get { return "0"; } }

        // Your custom format 
        public static string NumberDotTwoDigits { get { return "0.00"; } }

        public static string Currency { get { return "$#,##0.00;[Red]$#,##0.00"; } }
        public static string Accounting { get { return "_($* #,##0.00_);_($* (#,##0.00);_($* \" - \"??_);_(@_)"; } }
        public static string Date { get { return "m/d/yy"; } }
        public static string Time { get { return "[$-F400] h:mm:ss am/pm"; } }
        public static string Percentage { get { return "0.00%"; } }
        public static string Fraction { get { return "# ?/?"; } }
        public static string Scientific { get { return "0.00E+00"; } }
        public static string Text { get { return "@"; } }
        public static string Special { get { return ";;"; } }
        public static string Custom { get { return "#,##0_);[Red](#,##0)"; } }
}
NoWar
  • 36,338
  • 80
  • 323
  • 498
5

In Excel, you can set a Range.NumberFormat to any string as you would find in the "Custom" format selection. Essentially, you have two choices:

  1. General for no particular format.
  2. A custom formatted string, like "$#,##0", to specify exactly what format you're using.
DougM
  • 2,808
  • 17
  • 14
3

The .NET Library EPPlus implements a conversation from the string definition to the built in number. See class ExcelNumberFormat:

internal static int GetFromBuildIdFromFormat(string format)
{
    switch (format)
    {
        case "General":
            return 0;
        case "0":
            return 1;
        case "0.00":
            return 2;
        case "#,##0":
            return 3;
        case "#,##0.00":
            return 4;
        case "0%":
            return 9;
        case "0.00%":
            return 10;
        case "0.00E+00":
            return 11;
        case "# ?/?":
            return 12;
        case "# ??/??":
            return 13;
        case "mm-dd-yy":
            return 14;
        case "d-mmm-yy":
            return 15;
        case "d-mmm":
            return 16;
        case "mmm-yy":
            return 17;
        case "h:mm AM/PM":
            return 18;
        case "h:mm:ss AM/PM":
            return 19;
        case "h:mm":
            return 20;
        case "h:mm:ss":
            return 21;
        case "m/d/yy h:mm":
            return 22;
        case "#,##0 ;(#,##0)":
            return 37;
        case "#,##0 ;[Red](#,##0)":
            return 38;
        case "#,##0.00;(#,##0.00)":
            return 39;
        case "#,##0.00;[Red](#,#)":
            return 40;
        case "mm:ss":
            return 45;
        case "[h]:mm:ss":
            return 46;
        case "mmss.0":
            return 47;
        case "##0.0":
            return 48;
        case "@":
            return 49;
        default:
            return int.MinValue;
    }
}

When you use one of these formats, Excel will automatically identify them as a standard format.

ice1e0
  • 947
  • 7
  • 15