使用 With
语句,如何更新单元格 Range
中的 .value
,作为 Round 函数的结果,该函数会将各个 .value
作为参数。
换句话说,如何写:
Range("A1").Value = Round(Range("A1").Value, 0)
Range("A2").Value = Round(Range("A2").Value, 0)
以这种形式:
With Range("A1:A2")
.Value = Round(.Value, 0)
End With
我在某处看到过解决方案,但找不到。
最佳答案
Round
,或等效的 VBA.Round
,不接受数组作为其第一个参数。
使用 Application.Round
,它是 WorksheetFunction.Round
的后期绑定(bind)形式。
With Range("A1:A2")
.Value = Application.Round(.Value, 0)
End With
重要提示:VBA.Round
和 WorksheetFunction.Round
不执行相同的操作。 VBA.Round
使用银行家舍入,如其 docs 中所述。 。如果您需要银行四舍五入,那么您需要循环,但使用数组会更容易。
关于excel - VBA - 如何使用 `With` 语句舍入范围内的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74364544/