在 VBA 宏中使用符号 #(哈希)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10890892/
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
Use of symbol # (hash) in VBA Macro
提问by srinivasan
What is the meaning of the use of the #
symbol in Excel VBA?
#
Excel VBA中使用符号的含义是什么?
It is used like this:
它是这样使用的:
a = b /100#
I don't understand the significance of #
after the 100
?
我不明白?#
之后的意义100
?
回答by Siddharth Rout
The type-declaration character for Double is the number sign (#). Also called HASH
Double 的类型声明字符是数字符号 (#)。也称为哈希
Other type declaration characters are:
其他类型声明字符是:
- Integer %
- Long &
- Currency @
- Single !
- Double #
- String $
- 整数 %
- 长 &
- 货币 @
- 单身的 !
- 双倍的 #
- 字符串 $
Don't understand the significance of # here.
不明白这里#的意义。
It implies that when the expression is evaluated, the number in front of the type declaration character is treated as a specific data type instead of as a Variant.
这意味着在计算表达式时,类型声明字符前面的数字被视为特定的数据类型而不是 Variant。
See this example, which are basically the same.
看这个例子,基本上是一样的。
Sub Sample1()
Dim a#
a = 1.2
Debug.Print a
End Sub
Sub Sample2()
Dim a As Double
a = 1.2
Debug.Print a
End Sub
EDIT
编辑
Let me explain it a little more in detail.
让我更详细地解释一下。
Consider this two procedures
考虑这两个程序
Sub Sample1()
Dim a As Double, b As Integer
b = 32767
a = b * 100
Debug.Print a
End Sub
Sub Sample2()
Dim a As Double, b As Integer
b = 32767
a = b * 100#
Debug.Print a
End Sub
Question: One of them will fail. Can you guess which one?
问题:其中之一会失败。你能猜出是哪一个吗?
Ans: The 1st procedure Sub Sample1()
will fail.
答:第一个程序Sub Sample1()
将失败。
Reason:
原因:
In Sample2
, when you do b * 100#
the result of calculation will be of type Double
. Since it is within the limits of Double, so the calculation succeeds and the result is assigned to variable a
.
在Sample2
,当你做b * 100#
计算的结果将是类型Double
。由于在Double范围内,所以计算成功,结果赋值给变量a
。
Now in Sample1
, when you do b * 100
the result of calculation will be of type Integer
, since both the operands are of type integer. But the result of calculation exceeds the limits of Integer storage. As a result it will error out.
现在在 中Sample1
,当您进行b * 100
计算时,结果将是 类型Integer
,因为两个操作数都是整数类型。但是计算结果超出了Integer存储的限制。结果它会出错。
Hope it helps :)
希望能帮助到你 :)