在VBA宏中使用符号#(哈希) [英] Use of symbol # (hash) in VBA Macro
问题描述
在Excel VBA中使用#
符号的含义是什么?
What is the meaning of the use of the #
symbol in Excel VBA?
它被用于这个:
a = b /100#
在 100
之后,我不明白#
的意义?
I don't understand the significance of #
after the 100
?
推荐答案
Double的类型声明字符是数字符号(#)。也称为 HASH
The type-declaration character for Double is the number sign (#). Also called HASH
其他类型的声明字符是:
Other type declaration characters are:
- 整数%
- 长&
- 货币@
- 单! b $ b
- Double#
- String $
- Integer %
- Long &
- Currency @
- Single !
- Double #
- String $
不明白#在这里的意义。
Don't understand the significance of # here.
这意味着当表达式被评估时,前面的
类型声明字符被视为特定的数据类型,而不是
a Variant。
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.
看到这个例子,基本相同。 >
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
编辑
请详细解释一下。
考虑这个两个程序
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 :第一个程序 Sub Sample1()
将会失败。
Ans: The 1st procedure Sub Sample1()
will fail.
原因:
在 Sample2
,当您执行 b * 100#
计算结果将为 Double
。由于它是Double的限制,所以计算成功,结果被分配给变量 a
。
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
.
现在在 Sample1
中,当您执行 b * 100
计算结果将为整数
,因为这两个操作数都是整数类型。但计算结果超过了整数存储的限制。结果将会出错。
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.
希望它有帮助:)
这篇关于在VBA宏中使用符号#(哈希)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!