我有大约20,000个数据单元的工作量。该数据每200行属于不同的样本。我想将这些数据存储到一个三维数组中以便以后能够访问数据,我想做这样的事情:
lastCol = 15
n = 1
For i = 1 To 200 Step 199
sample(n) = sheets(1).range(Cells(i, lastCol), Cells(i + 199, lastCol)).Resize(200, lastCol)
n = n + 1
Next
*我知道我分配值的方式不正确,但我没有设法找到关于如何正确执行此操作的明确说明。
*再次澄清一下:我试图存储然后访问具有以下大小的矩阵:10x200x15,10个样本200行乘15列
答案 0 :(得分:1)
包含200(2D)范围变量的一维数组可能是更好的方法:
Sub Test()
Dim i As Long
Dim v As Variant
ReDim v(1 To 1)
For i = 1 To 20000 Step 200
If VarType(v(1)) <> vbVariant Then ReDim Preserve v(UBound(v) + 1)
Set v(UBound(v)) = ThisWorkbook.Worksheets("Sheet1").Range("A" & i).Resize(200, 15)
Debug.Print v(UBound(v)).Address
Next i
End Sub
Debug.Print
命令产生下面的输出,确认变量数组中的正确范围变量:
$A$1:$O$200
$A$201:$O$400
$A$401:$O$600
...
...
$A$19401:$O$19600
$A$19601:$O$19800
$A$19801:$O$20000
答案 1 :(得分:0)
另一个选项(如果你想要你提到的结构)将是读取数组中的范围,然后遍历它并将其放入所需的结构中,如下面的代码所示:
Sub Arrays()
Dim InArr As Variant
Dim OutArr() As String
Dim R As Long, rowcount As Long, C As Integer, colcount As Integer, Samp As Long
InArr = ThisWorkbook.Sheets(1).Range("A1:B2000").Value2
rowcount = UBound(InArr, 1)
colcount = UBound(InArr, 2)
ReDim OutArr(1 To Int((rowcount - 1) / 200) + 1, 1 To 200, 1 To colcount)
For R = 1 To rowcount
Samp = Int((R - 1) / 200) + 1
For C = 1 To colcount
OutArr(Samp, (R - 1) Mod 200 + 1, C) = InArr(R, C)
Next C
Next R
End Sub