VBA在单元格中写入数据非常慢

时间:2018-05-02 07:04:08

标签: excel vba performance loops

我写了一个非常直接的脚本,它将数据写入excel单元格。 基本上,这是一个数组循环,它将数据写入特定的单元格或公式。

问题是这部分脚本极其缓慢。 关于如何改进的想法?

感谢。

    For j = 0 To i - 1
    'Insère nouvelle ligne
    Rows(startRow & ":" & startRow).Select
    Selection.Copy
    Rows(startRow + 1 & ":" & startRow + 1).Select
    Selection.Insert Shift:=xlDown

    'Insère données
    If roomType(j) <> "" Then
        Feuil3.Cells(startRow, 1).Value = roomName(j)
        Feuil3.Cells(startRow, 2).Value = roomSurface(j)
        Feuil3.Cells(startRow, 7).Value = roomPeople(j)
        Feuil3.Cells(startRow, 12).Value = roomPeople(j)
        Feuil3.Cells(startRow, 5).Value = dict.Item(roomType(j))
        Feuil3.Cells(startRow, 3).Value = roomHeight(j)

        Feuil3.Range("F" & startRow).Formula = "=IFERROR(IF($E" & startRow & "=Data!$A$55,,ROUNDUP($B" & startRow & "/VLOOKUP($E" & startRow & ",Data!$A$3:$E$55,4,FALSE),0)),)"
        Feuil3.Range("H" & startRow).Formula = "=$C$25"
        Feuil3.Range("I" & startRow).Formula = "=IF($E" & startRow & "=Data!$A$55,$B" & startRow & "*$E$55,(MAX(F" & startRow & ",G" & startRow & ")*H" & startRow & "))"
        Feuil3.Range("N" & startRow).Formula = "=IFERROR(VLOOKUP($K" & startRow & ",$M$22:$O$26,3,FALSE),)"
        Feuil3.Range("O" & startRow).Formula = "=IFERROR(IF(ISBLANK(M" & startRow & ")=TRUE,L" & startRow & "*N" & startRow & ",L" & startRow & "*M" & startRow & "*N" & startRow & "),)"
        Feuil3.Range("Q" & startRow).Formula = "=MAX(I" & startRow & ",O" & startRow & ")"
        Feuil3.Range("T" & startRow).Formula = "=IFERROR(MAX(R" & startRow & ",S" & startRow & ")/(B" & startRow & "*C" & startRow & "),)"
    End If

    startRow = startRow + 1
Next j
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

2 个答案:

答案 0 :(得分:0)

将数组分配给一系列单元格比分配给循环中的单个单元格更快。你可以试试像Worksheets("MySheet").Range("A1:D100").Value = myArray这样的东西。这是因为VBA和Excel之间的通信需要一些时间。在这里再说一点:VBA Excel large data manipulation taking forever

同样使用Cells代替Range的速度提高了2.6倍。这是一个问题:Range() VS Cells() - run times

答案 1 :(得分:0)

我把剧本分成几部分。 这部分大部分时间都是

    For j = 0 To i - 2
    Feuil3.Cells(startRow, 5).Value = dict.Item(roomType(j))
    Feuil3.Cells(startRow, 6).Formula = "=IFERROR(IF($E" & startRow & "=Data!$A$55,,ROUNDUP($B" & startRow & "/VLOOKUP($E" & startRow & ",Data!$A$3:$E$55,4,FALSE),0)),)"
    Feuil3.Cells(startRow, 8).Formula = "=$C$25"
    Feuil3.Cells(startRow, 9).Formula = "=IF($E" & startRow & "=Data!$A$55,$B" & startRow & "*$E$55,(MAX(F" & startRow & ",G" & startRow & ")*H" & startRow & "))"
    Feuil3.Cells(startRow, 14).Formula = "=IFERROR(VLOOKUP($K" & startRow & ",$M$22:$O$26,3,FALSE),)"
    Feuil3.Cells(startRow, 15).Formula = "=IFERROR(IF(ISBLANK(M" & startRow & ")=TRUE,L" & startRow & "*N" & startRow & ",L" & startRow & "*M" & startRow & "*N" & startRow & "),)"
    Feuil3.Cells(startRow, 17).Formula = "=MAX(I" & startRow & ",O" & startRow & ")"
    Feuil3.Cells(startRow, 20).Formula = "=IFERROR(MAX(R" & startRow & ",S" & startRow & ")/(B" & startRow & "*C" & startRow & "),)"

    startRow = startRow + 1
Next j