我无法操纵变量数组

时间:2019-07-25 11:16:35

标签: vba

我无法操纵变量数组。

Dim ArrayCellsVar(3) As Variant
For X=1 To 3
 For Y=1 To 3
  For K=0 To 2
    ArrayCellsVar(K) = Array(Cells(Y + K * 4, X).Address(RowAbsolute:=False, ColumnAbsolute:=False))
  Next K
 Next Y
Next X
MsgBox ArrayCellsVar(0)

我希望数组中有一些单元格(例如A1,A3,A5),但是运行时错误为'9'

1 个答案:

答案 0 :(得分:0)

在我的评论中,我指的是对Cell对象的引用,而不仅仅是对其内容的引用。
这是一个示例,向您展示如何获取单元格的值,地址或单元格对象:

Dim ArrayCellsVarValue(3) As Variant
Dim ArrayCellsVarAddress(3) As String
Dim ArrayCellsVarCells(3) As Range
Dim x As Integer
Dim y As Integer
Dim k As Integer
For x = 1 To 3
    For y = 1 To 3
        For k = 0 To 2
            'Value
            ArrayCellsVarValue(k) = Cells(y + k * 4, x).Value
            'Address
            ArrayCellsVarAddress(k) = Cells(y + k * 4, x).Address(RowAbsolute:=False, ColumnAbsolute:=False)
            'The cell object itself
            Set ArrayCellsVarCells(k) = Cells(y + k * 4, x)
        Next k
    Next y
Next x
Debug.Print ArrayCellsVarValue(0)
Debug.Print ArrayCellsVarAddress(0)
Debug.Print ArrayCellsVarCells(0).Address & " contains [" & ArrayCellsVarCells(0).Value & "]"