VBA 数字格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10126419/
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
VBA number formatting
提问by Ian Gaida
I am writing a macro package to format numbers in accounting format so that I don't have to click "alt - h - fm" every time I want to make a number accounting format (which my job requires). I have the code up to a certain point, but it is not matching excel's accounting format. Right now the $ appears one space before the number, rather than at the left edge of the cell (like it does for excel's accounting format). My present code formats to thousands with no decimals, all that is wrong is that the dollar sign doesn't appear at the left edge of the cell.
我正在编写一个宏包来格式化会计格式的数字,这样每次我想制作数字会计格式(我的工作需要)时,我都不必单击“alt - h - fm”。我有一段代码,但它与 excel 的会计格式不匹配。现在 $ 出现在数字前一个空格,而不是在单元格的左边缘(就像 excel 的会计格式那样)。我目前的代码格式为千位,没有小数,唯一的错误是美元符号没有出现在单元格的左边缘。
Here is my code for the number format:
这是我的数字格式代码:
Selection.NumberFormat = "_(* $ #,##0_);_(* $ (#,##0);_(* ""$ -""?_);_(@_)"
How should I change it so that the dollar sign appears at the beginning of the cell?
我应该如何更改它以便美元符号出现在单元格的开头?
Thank you for the help! this has been stumping me for a while now...
感谢您的帮助!这已经让我难住了一段时间......
回答by assylias
You need to put the $before the *: _($* #,##0_);etc.
你需要把:等放在$前面。*_($* #,##0_);
Also, if you want to replicate an existing format, you can always use the macro recorder, change a cell to the format you want, and check the auto-generated code.
此外,如果您想复制现有格式,您始终可以使用宏记录器,将单元格更改为您想要的格式,并检查自动生成的代码。

