excel - 如何在VBA中使用As声明多个指定类型的变量?

标签 excel vba declaration variable-declaration

根据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 first As clause encountered after its variable name part.

但是,当我检查调试器或 MsgBox VarType(a) 输出时,情况并非如此。

enter image description here

如您所见,As 似乎仅适用于其之前的变量,即 cy。所有其他都是 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/

相关文章:

excel - 为什么从 Evaluate 调用时 VBA Find 循环失败?

excel - 自动调整用户表单大小

"[ ]) (char **)"的 C 数组

c++ - 替代函数语法差异

vba - 当电子表格中出现错误时,如何停止触发基于事件的 VBA 代码?

html - 如何在 VBA 网页抓取中从 HTML 代码中提取 <tspan> 元素

c# - 如何将 Parallel.ForEach 用于 Excel?

VBA 索引函数大小限制

excel - 打开第二个工作簿触发第一个工作簿中的 VBA 代码

javascript - 如何使用 JavaScript 调用此函数?