2017-07-08 90 views
0

我試圖把列的範圍的值放在一個2-dim數組中(0到1,0到'我列的範圍的長度')。excel-vba單列到多維數組

我需要的是這樣的:

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 

誰能告訴我,我必須使用什麼功能?

回答

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