只是想知道是否有人可以提供任何可能提高我的代码将数组写入工作簿的速度的建议。
我正在将大约 190 万行数据写入工作簿中的几张纸,一次一张。代码完成后,写入 excel 工作簿大约需要 18 个小时,这似乎过分了。这是设置。我这样打开工作簿:
Dim ExcelAp As Excel.Application
Dim ouputWorkbook As Excel.Workbook
Set ExcelAp = New Excel.Application
Set outputWorkbook = ExcelAp.Workbooks.Open("S:\Some Directory\Template.xlsx")
然后我将数组中工作簿的行加载到集合中,并循环遍历工作簿中的范围以复制数组:
For lonSheetOneCounter = 2 to 999999
    outputWorkbook.Worksheets(1).Range(_
        outputWorkbook.Worksheets(1).Cells(lonSheetOneCounter, 1).Address & ":" & _
        outputWorkbook.Worksheets(1).Cells(lonSheetOneCounter, 21).Address).Value = _
        outputCollection.item(lonSheetOneCounter - 1)
Next lonSheetOneCounter
其他纸张的复印方法相同。我已经使工作簿和 excel 实例不可见,我已将该工作簿的计算切换为手动,并且我还关闭了屏幕更新,但是完成复制到新工作簿仍然需要大约 18 个小时的时间。
我已经尝试为 entier 工作表制作一个二维数组,但是无论我使用哪种方法,当我尝试将该数组复制到工作簿时,都会出现“内存不足错误”。
我不确定是否还有什么我可以做的事情来解决错误并减少复制时间,但是如果有人有建议,我会全力以赴。就其价值而言,此宏位于另一个 excel 工作簿中,该工作簿在与我试图复制到的工作簿的单独 excel 实例中运行。
编辑:这里略有添加。我注意到我想引起注意的一些事情也让我认为有可能加快这个过程。我注意到宏逐渐变慢。前 X 行写入速度非常快,随着每一行的写入,接下来的行似乎越来越慢......
我将尝试一个实验,在该实验中,我设置模板以自动加载包含 100 万行已用行的电子表格……有点受底部建议的提示。我想知道 excel 是否必须为所有额外的行分配内存。也许如果我从一个已经设置了该行数的工作簿模板开始,我可能会更容易一些。
编辑:有人向我指出,我不清楚我正在阅读的数据来自哪里。使用 VBA 原语从多个文本文件中读取此数据。一个是管道分隔,另外两个逗号,并不是文件的方案有多大区别。
至于填充数组,这里是如何发生的片段。它看起来一团糟,但鉴于我正在比较的三个文件的格式,没有任何其他方法可以使数据匹配。无论如何,现在我将所有东西都放入大数组中,这就是我填充这些数组的方式。对 arrViLine 和 arrNonIraLine 和 arrIraLine 的引用只是文件的行从它们的原始管道和逗号分隔格式解析成的数组:
    If arrViLine(2) = arrIraLine(1) Or arrViLine(2) = arrNonIraLine(1) Then
        If arrViLine(2) = arrIraLine(1) Then
            boolVi = True
            boolIra = True
            boolNonIra = False
            If lonMatchCounter <= 999999 Then
                matchOneArray(lonMatchCounter, 1) = arrViLine(1)
                matchOneArray(lonMatchCounter, 2) = arrViLine(2)
                matchOneArray(lonMatchCounter, 3) = arrIraLine(2)
                matchOneArray(lonMatchCounter, 4) = arrIraLine(3)
                matchOneArray(lonMatchCounter, 5) = arrViLine(3)
                matchOneArray(lonMatchCounter, 6) = arrViLine(4)
                matchOneArray(lonMatchCounter, 7) = arrIraLine(4)
                matchOneArray(lonMatchCounter, 8) = arrViLine(6)
                matchOneArray(lonMatchCounter, 9) = arrViLine(5)
                matchOneArray(lonMatchCounter, 10) = arrViLine(7)
                matchOneArray(lonMatchCounter, 11) = arrViLine(8)
                matchOneArray(lonMatchCounter, 12) = arrViLine(9)
                matchOneArray(lonMatchCounter, 13) = arrViLine(10)
                matchOneArray(lonMatchCounter, 14) = arrViLine(11)
                matchOneArray(lonMatchCounter, 15) = arrViLine(12)
                matchOneArray(lonMatchCounter, 16) = arrIraLine(5)
                matchOneArray(lonMatchCounter, 17) = arrIraLine(6)
                matchOneArray(lonMatchCounter, 18) = arrViLine(13)
                matchOneArray(lonMatchCounter, 19) = arrViLine(14)
                matchOneArray(lonMatchCounter, 20) = "IRA"
                matchOneArray(lonMatchCounter, 21) = arrViLine(15)
                lonMatchCounter = lonMatchCounter + 1
            Else
                lonMatchTwoCounter = lonMatchCounter - 999999
                matchTwoArray(lonMatchTwoCounter, 1) = arrViLine(1)
                matchTwoArray(lonMatchTwoCounter, 2) = arrViLine(2)
                matchTwoArray(lonMatchTwoCounter, 3) = arrIraLine(2)
                matchTwoArray(lonMatchTwoCounter, 4) = arrIraLine(3)
                matchTwoArray(lonMatchTwoCounter, 5) = arrViLine(3)
                matchTwoArray(lonMatchTwoCounter, 6) = arrViLine(4)
                matchTwoArray(lonMatchTwoCounter, 7) = arrIraLine(4)
                matchTwoArray(lonMatchTwoCounter, 8) = arrViLine(6)
                matchTwoArray(lonMatchTwoCounter, 9) = arrViLine(5)
                matchTwoArray(lonMatchTwoCounter, 10) = arrViLine(7)
                matchTwoArray(lonMatchTwoCounter, 11) = arrViLine(8)
                matchTwoArray(lonMatchTwoCounter, 12) = arrViLine(9)
                matchTwoArray(lonMatchTwoCounter, 13) = arrViLine(10)
                matchTwoArray(lonMatchTwoCounter, 14) = arrViLine(11)
                matchTwoArray(lonMatchTwoCounter, 15) = arrViLine(12)
                matchTwoArray(lonMatchTwoCounter, 16) = arrIraLine(5)
                matchTwoArray(lonMatchTwoCounter, 17) = arrIraLine(6)
                matchTwoArray(lonMatchTwoCounter, 18) = arrViLine(13)
                matchTwoArray(lonMatchTwoCounter, 19) = arrViLine(14)
                matchTwoArray(lonMatchTwoCounter, 20) = "IRA"
                matchTwoArray(lonMatchTwoCounter, 21) = arrViLine(15)
                lonMatchCounter = lonMatchCounter + 1
            End If
        Else 'arrViLine(2) must = arrNonIraLine(1)
            boolVi = True
            boolIra = False
            boolNonIra = True
            If lonMatchCounter <= 999999 Then
                matchOneArray(lonMatchCounter, 1) = arrViLine(1)
                matchOneArray(lonMatchCounter, 2) = arrViLine(2)
                matchOneArray(lonMatchCounter, 3) = arrNonIraLine(2)
                matchOneArray(lonMatchCounter, 4) = arrNonIraLine(3)
                matchOneArray(lonMatchCounter, 5) = arrViLine(3)
                matchOneArray(lonMatchCounter, 6) = arrViLine(4)
                matchOneArray(lonMatchCounter, 7) = arrNonIraLine(5)
                matchOneArray(lonMatchCounter, 8) = arrViLine(6)
                matchOneArray(lonMatchCounter, 9) = arrViLine(5)
                matchOneArray(lonMatchCounter, 10) = arrViLine(7)
                matchOneArray(lonMatchCounter, 11) = arrViLine(8)
                matchOneArray(lonMatchCounter, 12) = arrViLine(9)
                matchOneArray(lonMatchCounter, 13) = arrViLine(10)
                matchOneArray(lonMatchCounter, 14) = arrViLine(11)
                matchOneArray(lonMatchCounter, 15) = arrViLine(12)
                matchOneArray(lonMatchCounter, 16) = arrNonIraLine(4)
                matchOneArray(lonMatchCounter, 17) = arrNonIraLine(6)
                matchOneArray(lonMatchCounter, 18) = arrViLine(13)
                matchOneArray(lonMatchCounter, 19) = arrViLine(14)
                matchOneArray(lonMatchCounter, 20) = "IRA"
                matchOneArray(lonMatchCounter, 21) = arrViLine(15)
                lonMatchCounter = lonMatchCounter + 1
            Else
                lonMatchTwoCounter = lonMatchCounter - 999999
                matchTwoArray(lonMatchTwoCounter, 1) = arrViLine(1)
                matchTwoArray(lonMatchTwoCounter, 2) = arrViLine(2)
                matchTwoArray(lonMatchTwoCounter, 3) = arrNonIraLine(2)
                matchTwoArray(lonMatchTwoCounter, 4) = arrNonIraLine(3)
                matchTwoArray(lonMatchTwoCounter, 5) = arrViLine(3)
                matchTwoArray(lonMatchTwoCounter, 6) = arrViLine(4)
                matchTwoArray(lonMatchTwoCounter, 7) = arrNonIraLine(5)
                matchTwoArray(lonMatchTwoCounter, 8) = arrViLine(6)
                matchTwoArray(lonMatchTwoCounter, 9) = arrViLine(5)
                matchTwoArray(lonMatchTwoCounter, 10) = arrViLine(7)
                matchTwoArray(lonMatchTwoCounter, 11) = arrViLine(8)
                matchTwoArray(lonMatchTwoCounter, 12) = arrViLine(9)
                matchTwoArray(lonMatchTwoCounter, 13) = arrViLine(10)
                matchTwoArray(lonMatchTwoCounter, 14) = arrViLine(11)
                matchTwoArray(lonMatchTwoCounter, 15) = arrViLine(12)
                matchTwoArray(lonMatchTwoCounter, 16) = arrNonIraLine(4)
                matchTwoArray(lonMatchTwoCounter, 17) = arrNonIraLine(6)
                matchTwoArray(lonMatchTwoCounter, 18) = arrViLine(13)
                matchTwoArray(lonMatchTwoCounter, 19) = arrViLine(14)
                matchTwoArray(lonMatchTwoCounter, 20) = "Non-IRA"
                matchTwoArray(lonMatchTwoCounter, 21) = arrViLine(15)
                lonMatchCounter = lonMatchCounter + 1
            End If
        End If
您也可以忽略布尔变量,它们用于提示宏是否应在下一个循环中读取特定文件的下一行。
编辑:并不是说它与我将数据写入 excel 的速度有多大关系,请将以下几行视为我正在使用的文件格式的示例。
“主文件:
Account Number|ID Number|Int Rate|Cum Int|Agreement|Type
12345|111111|.005|.01234|"C"|"IRA"
12346|111112|.005|.02345|"A"|"Non-IRA"
12347|111113|.004|.02345|"B"|"Non-IRA"
匹配文件一:
ID Number|Int Rate|Cum Int|Type
111111|.004|.01234|"IRA"
匹配文件二:
ID Number|Int Rate|Cum Int|Type
111113|.004|.02345|"Non-IRA"
这只是我正在使用的一个小例子。按 ID 号顺序列出的文本文件和 CSV 文件。在上面的示例中,宏将匹配主文件的第一行以匹配文件一个,并将来自两个文件的所有字段的数据记录到将输出到 Excel 电子表格的数组中。然后宏读入主文件的下一行并匹配文件 1,但将文件 2 的行转移到下一个循环。母版的下一行将没有匹配项,并记录在工作簿的单独工作表上。master 的最后一行匹配匹配文件二,并记录到与第一个匹配相同的数组中。
这就是例程的工作方式,不过,我遇到的真正问题是将数据写入 Excel 工作簿的速度。我目前正在将数据雕刻成列。
您不需要集合:只需将工作表中的数据分配到单个变体中,然后将变体分配回新工作表。
要最小化内存等,请尝试使用工作表上的 UsedRange。此示例一次复制一列:使用 Excel 2010 32 位将 100 万行乘 21 列从一张工作表复制到另一个工作表需要 35 秒
 Sub getting()
    Dim var As Variant
    Dim j As Long
    Dim dTime As Double
    dTime = Now
    For j = 1 To 21
        var = Worksheets("Sheet3").UsedRange.Resize(, 1).Offset(0, j - 1).Value2
        Worksheets("Sheet1").Range("a1").Resize(UBound(var), UBound(var, 2)).Offset(0, j - 1) = var
    Next j
    MsgBox CStr(Now - dTime)
End Sub
| 归档时间: | 
 | 
| 查看次数: | 4362 次 | 
| 最近记录: |