gpt4 book ai didi

Excel VBA四舍五入与 double

转载 作者:行者123 更新时间:2023-12-04 21:59:07 25 4
gpt4 key购买 nike

我正在尝试使用与我的工作表中的单元格相关的 double 来进行非常准确的计算。在测试时,我编写了以下代码:

Sub MacroTest()

Dim opt As Worksheet
Set opt = Sheets("Optimisation")

Dim test As Double

test = CDec(opt.Cells(2, 10))

End Sub

在单元格 J2 中是十进制值 - £3,298.8599993... 但是在调试代码时,很明显测试替身只选择 -3298.86 作为值。我不明白为什么会这样,因为我认为 double 比这更准确。我读过的大多数帖子似乎都认为使用 CDec 应该解决这个问题,但是如图所示,这似乎对我不起作用。

注意 - J2 中的值是使用公式计算的,不确定这是否有任何区别。非常感谢任何帮助 - 谢谢。

最佳答案

您应该使用 opt.Cells(2, 10).Value2在这种情况下。 Range.Value2 property :

The only difference between this property and the Value property is that the Value2 property doesn’t use the Currency and Date data types. You can return values formatted with these data types as floating-point numbers by using the Double data type.



所以默认 .Value将返回 Currency如果单元格格式为货币,而 .Value2将返回 Double尽管如此。

关于Excel VBA四舍五入与 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38503339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com