vba 强制 excel 将负数显示为 ($0,000)

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

forcing excel to display a negative number as ($0,000)

excelvba

提问by l--''''''---------''''''''''''

how do i do this in vba?

我如何在 vba 中做到这一点?

and how do i do this manually?

我如何手动执行此操作?

回答by HakonB

You can set the number format of a cell to a custom format like:

您可以将单元格的数字格式设置为自定义格式,例如:

ActiveCell.NumberFormat = "0;(0)"

That will put parentheses around negative numbers.

这将在负数周围加上括号。

If you want to display it exactly like ($0,000) you can do that with

如果你想像 ($0,000) 一样显示它,你可以用

ActiveCell.NumberFormat = "0;(""##代码##,000"")"

You can set the custom format manually through the Format Cells dialog.

您可以通过“单元格格式”对话框手动设置自定义格式。

回答by Ben

You can do it easily with a cell formula. =IF(A1<0, 0, A1)

您可以使用单元格公式轻松完成。=IF(A1<0, 0, A1)