5

我在 Excel 中有一些非常大的数据集,我需要对其进行解析 - 并且在数组中执行它比遍历工作表中的数据要快。将所有数据加载到数组中会导致内存问题(数据集那么大),因此我计划将数据的子集加载到数组中,对其进行处理,然后再加载另一个子集。我希望使用定义 LBound 和 UBound 的数组“功能”来帮助我跟踪我在工作表中的位置。但我发现将工作表值分配给数组会改变界限。以下代码演示了问题...

    Sub myTest3()
    Dim myRange As Range
    Dim myArray As Variant
    Dim myOffset As Long

        myOffset = 10
        Set myRange = Worksheets("RawData").Range("A1").CurrentRegion
        ReDim myArray(myOffset To myRange.Rows.Count, myRange.Columns.Count)
        MsgBox LBound(myArray, 1) & " to " & UBound(myArray)

        Set myRange = myRange.Offset(myOffset, 0).Resize(myRange.Rows.Count - myOffset, myRange.Columns.Count)

        myArray = myRange.Value2

        MsgBox LBound(myArray, 1) & " to " & UBound(myArray)

    End Sub

第一个 MsgBox 给了我“10 到 10931”。第二个 MsgBox 给了我“1 到 10921”。

关于维护我最初定义的数组边界的任何想法?我知道循环通过工作表进行分配会做到这一点,但它会很慢。

提前致谢。

4

1 回答 1

4

在这种情况下,Excel VBA 无法按您希望的方式工作。当你执行myArray = myRange.Value2时原来的内容myArray被替换了。Redim医疗阵列被扔掉了。Excel/VBA 不查看目标,而是替换它,或者更准确地说,它创建一个新数组并使myaArray变量指向它。

因此,您将需要更多代码才能到达您想去的地方。我会考虑将获取下一个块的代码放入一个单独的函数中并在那里进行簿记:

Function ChunkAtOffset(rng As Range, rowsInChunk As Long, colsInChunk As Long, offsetRows As Long) As Variant
' Note: doesn't cater for the case where there are fewer than 'offsetRows' in the target    
Dim arr As Variant, result As Variant
Dim r As Long, c As Long

    arr = rng.offset(offsetRows).Resize(rowsInChunk, colsInChunk).Value2

    ReDim result(offsetRows To offsetRows + rowsInChunk - 1, 1 To colsInChunk)

    For r = 1 To rowsInChunk
        For c = 1 To colsInChunk
            result(offsetRows - 1 + r, c) = arr(r, c)
        Next
    Next

    ChunkAtOffset = result

End Function

如果我运行这个:

Sub myTest4()

    Dim curReg As Range, ary As Variant, offset As Long
    With Range("A1")
        Set curReg = .CurrentRegion
        Do
            ary = ChunkAtOffset(.CurrentRegion, 10, .CurrentRegion.Columns.Count, offset)
            Debug.Print LBound(ary, 1) & " to " & UBound(ary)
            offset = offset + 10
        Loop Until offset >= .CurrentRegion.Rows.Count
    End With

End Sub

...我现在明白了:

0 to 9
10 to 19
20 to 29
于 2013-10-21T08:50:54.157 回答