1

由于我在 Excel 中的数据集超过了非常庞大的数量,我有两个程序都耗尽了内存。

Sub format()
Dim x, Y(), i&, j&, k&, s

x = Range("A1", Cells(1, Columns.count).End(xlToLeft)).Value
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For i = 1 To UBound(x, 2)
    .Item(x(1, i)) = i
Next i

x = Application.Trim(Range("BL3", Cells(Rows.count, "BL").End(xlUp)).Value)
ReDim Y(1 To UBound(x), 1 To .count): j = 1

For i = 1 To UBound(x)
    If InStr(x(i, 1), "==") = 0 Then
        s = Split(x(i, 1))

        If .Exists(s(0)) Then
        k = .Item(s(0)): Y(j, k) = mid(x(i, 1), Len(s(0)) + 2)
        End If
    Else
        j = j + 1
    End If
Next i
End With

[a2].Resize(j, UBound(Y, 2)).Value = Y()

End Sub

以上是我用来将一列数据拆分/修剪成多行/多列的过程。

我已将数据放入两列,每列由 60k 行组成,我需要做的是一旦通过 BL 读取,通过 BO 读取并继续从第一行开始将第二行数据放在新行下方完成的

4

1 回答 1

1

像这样的东西(未经测试)可能对你有用。它通过使用较小尺寸的块来避免创建巨大的二维数组。

Sub format()

Const BLOCK_SIZE As Long = 10000
Dim x, Y(), i&, j&, k&, s
Dim d As Object
Dim rOffset As Long
Dim xCount As Long

    Set d = CreateObject("Scripting.Dictionary")
    d.CompareMode = 1

    x = Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Value
    For i = 1 To UBound(x, 2)
          'using Add: you probably want this to error if duplicates exist...
          d.Add x(1, i), i         
    Next i

    x = Application.Trim(Range("BL3", Cells(Rows.Count, "BL").End(xlUp)).Value)
    xCount = UBound(x)
    rOffset = 0

    ReDim Y(1 To BLOCK_SIZE, 1 To d.Count)
    j = 1

    For i = 1 To xCount
        If InStr(x(i, 1), "==") = 0 Then
            s = Split(x(i, 1))

            If d.Exists(s(0)) Then
                k = d(s(0))
                Y(j, k) = Mid(x(i, 1), Len(s(0)) + 2)
            End If
        Else
            j = j + 1
            If j > BLOCK_SIZE Then
                [a2].Offset(rOffset, 0).Resize(BLOCK_SIZE, d.Count).Value = Y()
                ReDim Y(1 To BLOCK_SIZE, 1 To d.Count)
                j = 1
                rOffset = rOffset + BLOCK_SIZE
            End If
        End If
    Next i

    [a2].Offset(rOffset, 0).Resize(BLOCK_SIZE, d.Count).Value = Y()

End Sub
于 2012-06-13T20:32:30.700 回答