为什么 VBA Power Point 中的这段代码在没有数字 Dim 命令的情况下也能正常工作?
从 VBA 教程中,我了解到应该首先将包含数字的变量声明为整数:
Dim mynumber as integer
但是,请看一下这段代码:
Sub math()
A = 23
B = 2
ABSumTotal = A + B
strMsg = "The answer is " & "$" & ABSumTotal & "."
MsgBox strMsg
strMsg = "The answer is " & "$" & Sqr(ABSumTotal) & "."
MsgBox strMsg
End Sub
这里没有将变量声明为整数,但它仍然可以正常工作。为什么会这样呢?
From one of VBA tutorials I learned that variables contining numbers should be firstly declared as integers:
Dim mynumber as integer
But, please, look at this code:
Sub math()
A = 23
B = 2
ABSumTotal = A + B
strMsg = "The answer is " & "$" & ABSumTotal & "."
MsgBox strMsg
strMsg = "The answer is " & "$" & Sqr(ABSumTotal) & "."
MsgBox strMsg
End Sub
No variables are declared here as integer, but it still works just fine. Why is it so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,VB 不需要变量声明。这引起了很多挫败感,因为这意味着直到运行时出现问题之前,拼写错误才会被检测到。
要更改此设置,请将
Option Explicit
添加到文件顶部。By default, VB does not require variable declarations. This has caused lots of frustration because it means that typos go undetected until something breaks at runtime.
To change this, add
Option Explicit
to the top of the file.