arrays - 如何在Excel的vba中初始化多维数组变量

标签 arrays vba excel

Microsoft site建议以下代码应该可以工作:

暗数字 = {{1, 2}, {3, 4}, {5, 6}}

但是,当我尝试在 Excel VBA 模块中使用它时,出现编译错误。 以下内容适用于一维数组:

A = 数组(1, 2, 3, 4, 5)

但是我还没有找到一种方法对二维数组执行相同的操作。 有什么想法吗?

最佳答案

您还可以使用利用 Evaluate 函数和静态数组的速记格式。在下面的代码中,设置了 varData,其中 []Evaluate 函数的简写,而 {...} 表达式表示静态数组。每行由 ; 分隔,每个字段由 , 分隔。它使您得到与 simoco 代码相同的最终结果,但语法更接近您原来的问题:

Sub ArrayShorthand()

    Dim varData As Variant
    Dim intCounter1 As Integer
    Dim intCounter2 As Integer

    ' set the array
    varData = [{1, 2, 3; 4, 5, 6; 7, 8, 9}]

    ' test
    For intCounter1 = 1 To UBound(varData, 1)
        For intCounter2 = 1 To UBound(varData, 2)
            Debug.Print varData(intCounter1, intCounter2)
        Next intCounter2
    Next intCounter1

End Sub

关于arrays - 如何在Excel的vba中初始化多维数组变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24584088/

相关文章:

arrays - 数组向量的内存布局是什么?

PHP Array 语法和操作说明

excel - 在 VBA 中使用 '{' 字符

excel - 如何从 VBA 函数返回结果

excel - 将 Excel 图表复制到 Word 的 VBA 脚本在更高版本的 Word 中不起作用

arrays - 我将如何表达 Last Row + 10?

php - 在编写查询时需要帮助

vb.net 获取事件 excel 工作簿的完整路径和文件名

c# - 如何使用c#访问excel中的隐藏列

ios - 为什么我们应该在 Swift 中使用 "NSMutableArray"或 "NSMutableDictionary"?