Excel:在 VBA 中使用命名的 Range.NumberFormat:“常规”与“标准”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7289183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel: Working with named Range.NumberFormat in VBA: "General" vs. "Standard"
提问by downwitch
A couple questions about Range.NumberFormat here--hopefully someone has ready answers they can provide, as searching the web is tough with all the generic terms.
这里有几个关于 Range.NumberFormat 的问题——希望有人可以提供现成的答案,因为在网络上搜索所有通用术语都很困难。
A little background: I'm trying to eliminate "random" Error 1004codes: Unable to set the NumberFormat property of the Range classis the message. The error is occurring when I try to set the .NumberFormat of a late-boundobject to a named format, mainly working with "General" or "Standard". The object, in 99% of cases is Application.Selection. Also please note, the most common cause of this error--protection on the sheet or workbook--does not applyhere.
一点背景:我正在尝试消除“随机”错误 1004代码:无法设置 Range 类的 NumberFormat 属性是消息。当我尝试将后期绑定对象的 .NumberFormat 设置为命名格式时发生错误,主要使用“常规”或“标准”。在 99% 的情况下,对象是 Application.Selection。另请注意,此错误的最常见原因(工作表或工作簿上的保护)在此处不适用。
From Excel 2003 to 2007, Microsoft would appear to have introduced a bug in VBA. If in the immediate window I type
? Selection.NumberFormat
and hit Enter, I get "General". If I do the same thing with certain addins(not all, not predictable) running a macro with a breakpoint, I usually get "Standard". What is "Standard"? Where does that come from? I want to pick up a number format from one cell, and drop it on another; at random moments, I cannot apply "Standard" in this way, and its result is not like General. (In Excel 2003, it never appears in the immediate window, and fails systematically to apply.)Even if I convert all instances of "Standard" to "General", I still sometimes get an error on
MyObject.NumberFormat = "General"
. I have read in a couple of places that one is better off applying the underlying format of the named format, i.e. General applies to something (as I suppose standard does); what is the syntax to do that in Excel VBA?
从 Excel 2003 到 2007,Microsoft 似乎在 VBA 中引入了一个错误。如果在直接窗口中键入
? Selection.NumberFormat
并按 Enter,我会得到“常规”。如果我对运行带有断点的宏的某些插件(不是全部,不可预测)做同样的事情,我通常会得到“标准”。什么是“标准”?这是从哪里来的?我想从一个单元格中选取一种数字格式,然后将其放到另一个单元格中;在随机时刻,我不能以这种方式应用“标准”,其结果不像一般。(在 Excel 2003 中,它永远不会出现在直接窗口中,并且无法系统地应用。)即使我将“标准”的所有实例都转换为“常规”,我有时仍然会在
MyObject.NumberFormat = "General"
. 我在几个地方读到过,最好应用命名格式的基础格式,即 General 适用于某些东西(正如我认为标准所做的那样);在 Excel VBA 中执行此操作的语法是什么?
Thanks in advance for your help.
在此先感谢您的帮助。
回答by Banjoe
This probably isn't a full solution, but it's a start:
这可能不是一个完整的解决方案,但它是一个开始:
I believe "Standard" is what is used instead of "General" in certain languages. You should compare your results with .NumberFormatLocal.
我相信在某些语言中使用“标准”而不是“一般”。您应该将您的结果与 .NumberFormatLocal 进行比较。
For the solution part, if you're trying to set NumberFormat to "General" and getting errors then just set the NumberForamt to an empty string. This will automatically make the NumberFormat General/Standard.
对于解决方案部分,如果您尝试将 NumberFormat 设置为“General”并出现错误,则只需将 NumberForamt 设置为空字符串。这将自动使 NumberFormat 成为通用/标准。
selection.NumberFormat = ""