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

use*_*110 48 excel vba excel-vba excel-2010

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

在此输入图像描述

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

.NumberFormat ="@"
Run Code Online (Sandbox Code Playgroud)

或者号码:

.NumberFormat = "0.00000"
Run Code Online (Sandbox Code Playgroud)

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

doo*_*ers 72

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

宏:

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

结果:

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)
Run Code Online (Sandbox Code Playgroud)

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

  • 会计需要转义,""以防止使用该代码的vba中的错误. (3认同)
  • 就我个人而言,我更喜欢 & chr(34) & - 它更干净 (2认同)

Kev*_*ner 18

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


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


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

我手动将单元格格式化为中国(PRC)货币

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

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

打开

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

单击自定义

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

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

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

_ [$¥-804]* #,##0.00_ ;_ [$¥-804]* -#,##0.00_ ;_ [$¥-804]* "-"??_ ;_ @_ 
Run Code Online (Sandbox Code Playgroud)

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

我希望这有帮助.

  • 我尝试了你的方法,但虽然我确实弄清楚了我想要的格式,但世界仍然不是我的。请指教 ;) (2认同)
  • 你是100%正确的。我年轻时的乐观情绪已经不老了! (2认同)

Dev*_*per 7

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

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)"; } }
}
Run Code Online (Sandbox Code Playgroud)


Dou*_*ugM 5

在Excel中,您可以设置Range.NumberFormat为“自定义”格式选择中的任何字符串。本质上,您有两种选择:

  1. 一般,无特定格式。
  2. 自定义格式的字符串,例如“ $#,## 0”,用于确切指定您使用的格式。