根据this documentation by Microsoft ,可以用下面的代码来确认;
a, b, and c are all Single; x and y are both Double
Dim a, b, c As Single, x, y As Double, i As Integer
> a, b, and c are all Single; x and y are both Double
其背后的逻辑如下
You can specify different data types for different variables by using a separate
As
clause for each variable you declare. Each variable takes the data type specified in the firstAs
clause encountered after its variable name part.
但是,当我检查调试器或 MsgBox VarType(a)
输出时,情况并非如此。
如您所见,As
似乎仅适用于其之前的变量,即 c
、y
和我
。所有其他都是 Variant/Empty,并且 VarType
返回 0。
这只是文档错误,还是我遗漏了一些明显的东西?
Microsoft Visual Basic 应用程序 7.1.1056 Excel 2016(Windows 10)
最佳答案
您链接到的文档没有错误,但它是为 VB.NET 而不是 VBA 编写的。
在 VBA 中,正如您所观察到的,任何未紧跟 As <type>
的变量声明将是Variant
。
因此你需要写:
Dim a As Single, b As Single, c As Single, x As Double, y As Double, i As Integer
关于excel - 如何在VBA中使用As声明多个指定类型的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55059988/