Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VBA中存储工作表数据的三维数组_Vba_Excel - Fatal编程技术网

在VBA中存储工作表数据的三维数组

在VBA中存储工作表数据的三维数组,vba,excel,Vba,Excel,我有一个工作表,里面有大约20000个单元格的数据。这些数据每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 *我知道我

我有一个工作表,里面有大约20000个单元格的数据。这些数据每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列

一个包含200(2D)个范围变量的1D数组可能是更好的方法:

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
另一种选择(如果您想要的正是您提到的结构)是读取数组中的范围,然后循环遍历它并将其放入所需的结构中,如以下代码所示:

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

你是说
100*200*15
as
100*200=20000
?你给我的答案中有些东西我不太明白,但是,它确实很好。我只是不知道现在如何访问数据。比如说,我想访问行“row”和列“col”中的示例“n”,然后对数据进行一些计算并将其写入单元格。我希望类似这样的东西:sample(n,row,col)访问数据,因为sample(n)表示一个
范围
对象(200行15列),您可以这样访问它:
sample(n)。Cells(row,col)
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