在 VBA 宏中使用符号 #(哈希) [英] Use of symbol # (hash) in VBA Macro
问题描述
Excel VBA中#
符号的使用是什么意思?
它是这样使用的:
a = b/100#
我不明白100
后面#
的意义?
Double 的类型声明字符是数字符号 (#).也称为 HASH
其他类型声明字符有:
- 整数%
- 龙&
- 货币@
- 单身!
- 双#
- 字符串 $
<块引用>
不明白这里#的意义.
这意味着当表达式被计算时,前面的数字类型声明字符被视为特定的数据类型而不是作为一个变体.
看这个例子,基本相同.
Sub Sample1()昏暗#一 = 1.2调试.打印一个结束子子样本2()调暗为双一 = 1.2调试.打印一个结束子
编辑
让我更详细地解释一下.
考虑这两个过程
Sub Sample1()Dim a As Double, b As Integerb = 32767a = b * 100调试.打印一个结束子子样本2()Dim a As Double, b As Integerb = 32767a = b * 100#调试.打印一个结束子
问题:其中一个会失败.你能猜出是哪一个吗?
Ans:第一个过程 Sub Sample1()
将失败.
原因:
在Sample2
中,当你做b * 100#
时,计算结果将是Double
类型.由于在Double范围内,所以计算成功,结果赋值给变量a
.
现在在 Sample1
中,当你做 b * 100
时,计算结果将是 Integer
类型,因为两个操作数都是输入整数.但是计算结果超出了Integer存储的限制.结果会出错.
希望有帮助:)
What is the meaning of the use of the #
symbol in Excel VBA?
It is used like this:
a = b /100#
I don't understand the significance of #
after the 100
?
The type-declaration character for Double is the number sign (#). Also called HASH
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.
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.
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
.
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.
Hope it helps :)
这篇关于在 VBA 宏中使用符号 #(哈希)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!