使用 VBA 四舍五入到小数点后两位

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

Rounding to 2 decimal places using VBA

excel-vbarounding-errorvbaexcel

提问by Mike

I am writing a program that sums information I enter by month (column) and by person (row), but I am having trouble with a rounding issue. I have seen other places that it is possible to name a range, then format the cells in that range to round to 2 decimal places, but I am new to VBA and the pseudo code the answer was written in wasn't enough to help me.

我正在编写一个程序来汇总我按月(列)和按人(行)输入的信息,但我在舍入问题上遇到了麻烦。我已经看到其他地方可以命名一个范围,然后将该范围内的单元格格式化为四舍五入到小数点后两位,但我是 VBA 新手,并且编写答案的伪代码不足以帮助我.

Below is the link to the answer I found that fits my problem, but I am having trouble implementing the pseudo code.

下面是我发现适合我的问题的答案的链接,但我在实现伪代码时遇到了问题。

VBA Round Function

VBA 轮函数

回答by dcp

Use the VBA round function, see here: http://www.techonthenet.com/excel/formulas/round_vba.php

使用 VBA 轮函数,请参见此处:http: //www.techonthenet.com/excel/formulas/round_vba.php

But if you just want the columns to be shown as two decimal places, why not just set the format of the columns to be be 2 decimal places? There's no need to use VBA code. Here are the instructions on how to do that:

但是,如果您只想将列显示为两位小数,为什么不将列的格式设置为两位小数呢?无需使用 VBA 代码。以下是有关如何执行此操作的说明:

  1. Highlight the entire column
  2. Right click, choose "Format Cells...",
  3. Click the Number tab, and then choose "Custom" for the Category.
  4. Choose 0.00 for the Type.
  5. Now try typing 1.2345 in one of the cells in this column, and when you leave the cell it will be 1.23.
  1. 突出显示整个列
  2. 右键单击,选择“设置单元格格式...”,
  3. 单击数字选项卡,然后为类别选择“自定义”。
  4. 选择 0.00 作为类型。
  5. 现在尝试在此列的一个单元格中键入 1.2345,当您离开该单元格时,它将是 1.23。