excel-vba单列到多维数组

时间:2017-07-08 00:24:48

标签: excel-vba vba excel

我正在尝试将列范围的值放在2维数组中(0到1,0'到我列的范围的长度')。

我需要这样的东西:

arr(0, 0) = value of B4
arr(1, 0) = value of B5
arr(0, 1) = value of B6
arr(1, 1) = value of B7
...
arr(0, n) = value of Bn
arr(1, last row of my column range) = value of last B

有人可以告诉我必须使用哪种功能吗?

1 个答案:

答案 0 :(得分:0)

以下代码循环遍历B列中的每个单元格,从B4开始,并将它们的值分配给数组aResults,如您所述...

Dim aResults() As Variant
Dim rRange As Range
Dim nCols As Long
Dim i As Long

Set rRange = Range("B4:B" & Cells(Rows.Count, "B").End(xlUp).Row)

nCols = rRange.Cells.Count \ 2
nCols = nCols + (rRange.Cells.Count Mod 2)

ReDim aResults(1, nCols - 1)

For i = 0 To rRange.Cells.Count - 1
    'Debug.Print i Mod 2, i \ 2, rRange(1).Offset(i).Value
    aResults(i Mod 2, i \ 2) = rRange(1).Offset(i).Value
Next i