Excel VBA中的.NumberFormat选项是什么?

What are .NumberFormat Options In Excel VBA?

你能告诉我Excel VBA中的.NumberFormat格式选项吗? 如您所知,Excel 2010支持以下类型:

enter image description here

我知道我们可以将示例文本类型设置为:

1
.NumberFormat ="@"

或者号码:

1
.NumberFormat ="0.00000"

能告诉我VBA中其他类型的选项吗?


请注意,这是在Excel for Mac 2011上完成的,但对于Windows应该是相同的

宏:

1
2
3
4
5
6
7
Sub numberformats()
  Dim rng As Range
  Set rng = Range("A24:A35")
  For Each c In rng
    Debug.Print c.NumberFormat
  Next c
End Sub

结果:

1
2
3
4
5
6
7
8
9
10
11
12
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)

(我刚选择了一个随机的自定义条目)


感谢这个问题(和答案),我发现了一种简单的方法来获取Excel提供的几乎任何格式的确切NumberFormat字符串。

如何获取任何Excel数字格式的NumberFormat字符串

步骤1:在用户界面中,将单元格设置为要使用的NumberFormat。

I manually formatted a cell to Chinese (PRC) currency

在我的示例中,我从"帐号格式"组合框中包含的选项中选择了中国(PRC)货币。

步骤2:展开Number Format下拉列表并选择"More Number Formats ..."。

Open the Number Format dropdown

步骤3:在"数字"选项卡的"类别"中,单击"自定义"。

Click Custom

"示例"部分显示我应用的中文(PRC)货币格式。

"类型"输入框包含可以以编程方式使用的NumberFormat字符串。

因此,在此示例中,我的中国(PRC)货币单元格的NumberFormat如下:

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

如果你为你想要的每个NumberFormat做了这些步骤,那么世界就是你的。

我希望这有帮助。


dovers给了我很好的答案,在此基础上你可以尝试使用它

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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)"; } }
}

在Excel中,您可以将Range.NumberFormat设置为任何字符串,就像在"自定义"格式选择中找到的那样。基本上,您有两个选择:

  • 一般没有特定的格式。
  • 自定义格式化字符串,如"$#,## 0",用于指定您正在使用的格式。

  • .NET库EPPlus实现从字符串定义到内置编号的对话。
    请参阅ExcelNumberFormat类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    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;
        }
    }

    当您使用这些格式之一时,Excel将自动将它们标识为标准格式。