【问题标题】:What are .NumberFormat Options In Excel VBA?Excel VBA 中的 .NumberFormat 选项是什么?
【发布时间】:2013-12-18 01:36:18
【问题描述】:

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

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

.NumberFormat ="@"

或数字:

.NumberFormat = "0.00000"

能否请您告诉我 VBA 中类型的其他选项是什么?

【问题讨论】:

  • 为什么不录制宏并检查一下?
  • Format Cell对话框中,点击你想要的格式类型(例如,'Accounting), and then click Custom. The list on the bottom right shows the current Accounting`格式字符串(以及其他)。上面的文本框显示了你该项目的NumberFormat 字符串。您可以直接从该文本框复制/粘贴到代码中的字符串中。
  • 谢谢肯,这正是我要找的
  • 这是我创建的一个有点愚蠢但色彩丰富的自定义格式预览网页:yoursumbuddy.com/preview-excel-custom-formats

标签: excel vba


【解决方案1】:

请注意,这是在 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

结果:

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)

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

【讨论】:

  • Accounting 的 "s 需要转义,"" 以防止使用该代码的 vba 中出现错误。
  • 我需要知道你所说的“逃脱”是什么意思。您是说我们需要双引号 (-) 才能使其在 VBA 中工作?
  • 就个人而言,我更喜欢 & chr(34) & - 它更干净
【解决方案2】:

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


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


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

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

第 2 步:展开数字格式下拉菜单并选择“更多数字格式...”。

第 3 步:在“数字”选项卡的“类别”中,点击“自定义”。

“示例”部分显示了我应用的中国 (PRC) 货币格式。

“类型”输入框包含您可以通过编程方式使用的 NumberFormat 字符串。

所以,在这个例子中,我的中国(PRC)货币单元格的 NumberFormat 如下:

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

如果您为所需的每个 NumberFormat 执行这些步骤,那么世界就是您的了。

我希望这会有所帮助。

【讨论】:

  • 我尝试了你的方法,但虽然我确实找到了我想要的格式,但世界仍然不是我的。请指教;)
  • 你是 100% 正确的。我年轻时的乐观情绪并没有很好地老化!
【解决方案3】:

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)"; } }
}

【讨论】:

    【解决方案4】:

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

    1. General 没有特定的格式。
    2. 自定义格式的字符串,如“$#,##0”,用于准确指定您使用的格式。

    【讨论】:

      【解决方案5】:

      .NET 库EPPlus 实现了从字符串定义到内置数字的对话。 见班级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;
          }
      }
      

      当您使用其中一种格式时,Excel 会自动将它们识别为标准格式。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-20
        • 2018-08-02
        • 1970-01-01
        • 1970-01-01
        • 2011-07-07
        • 2012-10-10
        • 1970-01-01
        相关资源
        最近更新 更多