vba 使用VBA为负数设置带有千位分隔符、2位小数和减号的标准数字格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28066255/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 05:54:57  来源:igfitidea点击:

Set standard number format with thousand separator, 2 decimals and minus sign for negative numbers using VBA?

vbaexcel-vbaexcel

提问by user1283776

I've seen the question asked before on stackoverflow, how to get normal number format with thousand separator and 2 decimals. The answer was to set:

我在stackoverflow上看到过之前问过的问题,如何获得带有千位分隔符和2位小数的正常数字格式。答案是设置:

    rng.NumberFormat = "##0.00"

But this is incomplete, because, at least on my computer, I don't get any space separator between millions and thousands. So I have changed that to:

但这并不完整,因为至少在我的计算机上,我没有在数百万和数千之间使用任何空格分隔符。所以我把它改成:

    rng.NumberFormat = "### ### ##0.00"

But this is again incomplete, because for some reason negative numbers were formatted to look like they have a space between the minus sign and the number. See below:

但这又是不完整的,因为由于某种原因,负数被格式化为看起来在减号和数字之间有一个空格。见下文:

- 12.4

So, there are some things left to do to arrive at Excels "built-in" "format as number" formats. Additionally the formatting that I apply though VBA is described as Custom by Excel.

因此,要实现 Excel 的“内置”“格式为数字”格式,还需要做一些事情。此外,我通过 VBA 应用的格式被 Excel 描述为自定义。

Is there any way to set the format to be the standard built in format as number with thousand separators, 2 decimals and minus signs for negative numbers?

有什么方法可以将格式设置为标准内置格式,即带有千位分隔符、2 位小数和负数减号的数字?

I'm looking for something like:

我正在寻找类似的东西:

    rng.NumberFormat = "Number, 2, minus"

采纳答案by simon at rcl

rng.NumberFormat = "# ##0.00:-# ##0.00"

You put the format for positive numbers before : and the format for negative after. You don't need to put hundreds of # signs in the format, just enough to show what the 1000's separator is.

您将正数的格式放在 : 之前,将负数的格式放在之后。您不需要在格式中放置数百个 # 符号,足以显示 1000 的分隔符是什么。