Excel:在 VBA 中使用命名的 Range.NumberFormat:“常规”与“标准”相比
这里有几个关于 Range.NumberFormat 的问题 - 希望有人可以提供现成的答案,因为在网上搜索所有通用术语非常困难。
一点背景知识:我试图消除“随机”错误 1004 代码:无法设置 Range 类的 NumberFormat 属性 是消息。当我尝试将后期绑定对象的.NumberFormat设置为命名格式(主要使用“常规”或“标准”)时,会发生错误。在 99% 的情况下,对象是 Application.Selection。另请注意,导致此错误的最常见原因(工作表或工作簿上的保护)在此不适用。
从 Excel 2003 到 2007,Microsoft 似乎在 VBA 中引入了错误。如果在立即窗口中输入
? Selection.NumberFormat
并按 Enter 键,我得到“常规”。如果我对某些插件(不是全部,不可预测)执行同样的操作,运行带有断点的宏,我通常会得到“标准”。什么是“标准”?这是从哪里来的?我想从一个单元格中选取一种数字格式,然后将其放在另一个单元格上;在随机时刻,我无法以这种方式应用“标准”,并且其结果不像一般。 (在 Excel 2003 中,它永远不会出现在即时窗口中,并且无法系统地应用。)即使我将“Standard”的所有实例转换为“General”,我有时仍然会在
MyObject 上遇到错误。 NumberFormat =“常规”
。我在几个地方读到,最好应用命名格式的底层格式,即“通用”适用于某些东西(正如我认为标准所做的那样);在 Excel VBA 中执行此操作的语法是什么?
预先感谢您的帮助。
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.
A little background: I'm trying to eliminate "random" Error 1004 codes: Unable to set the NumberFormat property of the Range class is the message. The error is occurring when I try to set the .NumberFormat of a late-bound object 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 apply here.
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?
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能不是一个完整的解决方案,但它是一个开始:
我相信在某些语言中使用“标准”而不是“常规”。您应该将结果与 .NumberFormatLocal 进行比较。
对于解决方案部分,如果您尝试将 NumberFormat 设置为“常规”并收到错误,则只需将 NumberForamt 设置为空字符串即可。这将自动使 NumberFormat 成为通用/标准。
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.
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.