vba VBA如何声明为十进制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30917438/
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
VBA How to declare as Decimal
提问by Thomas Shera
I'm optimizing a macro in VBA which had none of the data types declared, so everything was clumsily treated by the compiler as a variant. I'm dealing with scientific measurements, so I need precision. How do I declare "Dim decAsdf as Decimal" (not like that but the proper way)?
我正在优化 VBA 中没有声明任何数据类型的宏,因此编译器将所有内容都笨拙地视为变体。我正在处理科学测量,所以我需要精确度。我如何声明“Dim decAsdf 为十进制”(不是那样,而是正确的方式)?
回答by Rory
You can't declare a variable as Decimal- you have to use Variant(you can use CDecto populate it with a Decimaltype though).
您不能将变量声明为Decimal- 您必须使用Variant(尽管您可以使用类型CDec填充它Decimal)。
回答by daytonrazorback
The best way is to declare the variable as a Singleor a Doubledepending on the precision you need. The data type Singleutilizes 4 Bytes and has the range of -3.402823E38 to 1.401298E45. Doubleuses 8 Bytes.
最好的方法是根据您需要的精度将变量声明为 aSingle或 a Double。数据类型Single使用 4 个字节,范围为 -3.402823E38 到 1.401298E45。 Double使用 8 个字节。
You can declare as follows:
您可以声明如下:
Dim decAsdf as Single
or
或者
Dim decAsdf as Double
Here is an example which displays a message box with the value of the variable after calculation. All you have to do is put it in a module and run it.
这是一个示例,它显示一个消息框,其中包含计算后的变量值。你所要做的就是把它放在一个模块中并运行它。
Sub doubleDataTypeExample()
Dim doubleTest As Double
doubleTest = 0.0000045 * 0.005 * 0.01
MsgBox "doubleTest = " & doubleTest
End Sub

