VBA代码在excel中执行需要很长时间

Posted

技术标签:

【中文标题】VBA代码在excel中执行需要很长时间【英文标题】:VBA code takes long time to execute in excel 【发布时间】:2018-02-19 18:28:29 【问题描述】:

以下 VBA 代码需要很长时间才能执行

Sub LOOK()

Worksheets("Input").Unprotect ("ds12345")
Dim found As Range
Set found = Sheets("Records").Columns("D").Find(What:=ActiveSheet.Cells(3, 13).Value, SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole)
If found Is Nothing Then
    MsgBox "Not found"
Else
    MsgBox "Found on row " & found.Row
End If

    Dim iRow As Integer

    iRow = InputBox("Which row?")

    Worksheets("Input").Cells(2, 1).Value = Worksheets("Records").Cells(iRow, 2).Value
    Worksheets("Input").Cells(4, 2).Value = Worksheets("Records").Cells(iRow, 3).Value
    Worksheets("Input").Cells(3, 13).Value = Worksheets("Records").Cells(iRow, 4).Value
    Worksheets("Input").Cells(4, 13).Value = Worksheets("Records").Cells(iRow, 5).Value
    Worksheets("Input").Cells(10, 3).Value = Worksheets("Records").Cells(iRow, 6).Value
    Worksheets("Input").Cells(3, 16).Value = Worksheets("Records").Cells(iRow, 7).Value
    Worksheets("Input").Cells(9, 7).Value = Worksheets("Records").Cells(iRow, 9).Value
    Worksheets("Input").Cells(12, 7).Value = Worksheets("Records").Cells(iRow, 10).Value
    Worksheets("Input").Cells(13, 7).Value = Worksheets("Records").Cells(iRow, 11).Value
    Worksheets("Input").Cells(9, 14).Value = Worksheets("Records").Cells(iRow, 12).Value
    Worksheets("Input").Cells(94, 5).Value = Worksheets("Records").Cells(iRow, 13).Value
    Worksheets("Input").Cells(95, 5).Value = Worksheets("Records").Cells(iRow, 14).Value
    Worksheets("Input").Cells(97, 2).Value = Worksheets("Records").Cells(iRow, 15).Value
    Worksheets("Input").Cells(20, 1).Value = Worksheets("Records").Cells(iRow, 16).Value
    Worksheets("Input").Cells(21, 1).Value = Worksheets("Records").Cells(iRow, 17).Value
    Worksheets("Input").Cells(22, 1).Value = Worksheets("Records").Cells(iRow, 18).Value
    Worksheets("Input").Cells(23, 1).Value = Worksheets("Records").Cells(iRow, 19).Value
    Worksheets("Input").Cells(24, 1).Value = Worksheets("Records").Cells(iRow, 20).Value
    Worksheets("Input").Cells(25, 1).Value = Worksheets("Records").Cells(iRow, 21).Value
    Worksheets("Input").Cells(26, 1).Value = Worksheets("Records").Cells(iRow, 22).Value
    Worksheets("Input").Cells(27, 1).Value = Worksheets("Records").Cells(iRow, 23).Value
    Worksheets("Input").Cells(28, 1).Value = Worksheets("Records").Cells(iRow, 24).Value
    Worksheets("Input").Cells(29, 1).Value = Worksheets("Records").Cells(iRow, 25).Value
    Worksheets("Input").Cells(30, 1).Value = Worksheets("Records").Cells(iRow, 26).Value
    Worksheets("Input").Cells(31, 1).Value = Worksheets("Records").Cells(iRow, 27).Value
    Worksheets("Input").Cells(32, 1).Value = Worksheets("Records").Cells(iRow, 28).Value
    Worksheets("Input").Cells(33, 1).Value = Worksheets("Records").Cells(iRow, 29).Value
    Worksheets("Input").Cells(34, 1).Value = Worksheets("Records").Cells(iRow, 30).Value
    Worksheets("Input").Cells(20, 11).Value = Worksheets("Records").Cells(iRow, 31).Value
    Worksheets("Input").Cells(21, 11).Value = Worksheets("Records").Cells(iRow, 32).Value
    Worksheets("Input").Cells(22, 11).Value = Worksheets("Records").Cells(iRow, 33).Value
    Worksheets("Input").Cells(23, 11).Value = Worksheets("Records").Cells(iRow, 34).Value
    Worksheets("Input").Cells(24, 11).Value = Worksheets("Records").Cells(iRow, 35).Value
    Worksheets("Input").Cells(42, 11).Value = Worksheets("Records").Cells(iRow, 37).Value
    Worksheets("Input").Cells(43, 11).Value = Worksheets("Records").Cells(iRow, 38).Value
    Worksheets("Input").Cells(44, 11).Value = Worksheets("Records").Cells(iRow, 39).Value
    Worksheets("Input").Cells(45, 11).Value = Worksheets("Records").Cells(iRow, 40).Value
    Worksheets("Input").Cells(46, 11).Value = Worksheets("Records").Cells(iRow, 41).Value
    Worksheets("Input").Cells(47, 11).Value = Worksheets("Records").Cells(iRow, 42).Value
    Worksheets("Input").Cells(48, 11).Value = Worksheets("Records").Cells(iRow, 43).Value
    Worksheets("Input").Cells(58, 5).Value = Worksheets("Records").Cells(iRow, 44).Value
    Worksheets("Input").Cells(59, 5).Value = Worksheets("Records").Cells(iRow, 45).Value
    Worksheets("Input").Cells(60, 5).Value = Worksheets("Records").Cells(iRow, 46).Value
    Worksheets("Input").Cells(58, 8).Value = Worksheets("Records").Cells(iRow, 47).Value
    Worksheets("Input").Cells(59, 8).Value = Worksheets("Records").Cells(iRow, 48).Value
    Worksheets("Input").Cells(60, 8).Value = Worksheets("Records").Cells(iRow, 49).Value
    Worksheets("Input").Cells(58, 11).Value = Worksheets("Records").Cells(iRow, 50).Value
    Worksheets("Input").Cells(59, 11).Value = Worksheets("Records").Cells(iRow, 51).Value
    Worksheets("Input").Cells(60, 11).Value = Worksheets("Records").Cells(iRow, 52).Value
    Worksheets("Input").Cells(58, 14).Value = Worksheets("Records").Cells(iRow, 53).Value
    Worksheets("Input").Cells(59, 14).Value = Worksheets("Records").Cells(iRow, 54).Value
    Worksheets("Input").Cells(60, 14).Value = Worksheets("Records").Cells(iRow, 55).Value
    Worksheets("Input").Cells(65, 18).Value = Worksheets("Records").Cells(iRow, 56).Value
    Worksheets("Input").Cells(104, 18).Value = Worksheets("Records").Cells(iRow, 76).Value

    If [l8] = [n9] Then
        [K8] = "YES"
        End If
    If [l9] = [n9] Then
        [K9] = "YES"
        End If
    If [l10] = [n9] Then
        [K10] = "YES"
        End If
    If [l12] = [n9] Then
        [K12] = "YES"
        End If
    If [l13] = [n9] Then
        [K13] = "YES"
        End If
    If [l14] = [n9] Then
        [K14] = "YES"
        End If
    If [l15] = [n9] Then
        [K15] = "YES"
        End If
    If [l16] = [n9] Then
        [K16] = "YES"
        End If
    If [l17] = [n9] Then
        [K17] = "YES"
    End If

Worksheets("Input").Protect ("ds12345")
End Sub

如果可以修改此代码以快速运行。或者,如果需要编写任何替代 VBA 代码以使其快速从工作表“输入”运行以从工作表“记录”中提取数据。谢谢

【问题讨论】:

如果有效,则属于代码审查:codereview.stackexchange.com 如果无效,请说明问题出在哪里。 @ScottCraner:请允许我有所不同 - 编写的代码存在一个地方性问题,这会导致性能受到多个数量级的惩罚;我相信遇到这个问题的金融分析师会向 Stack Overflow 而非 Code Review 寻求帮助。 @PieterGeerkens 他们会看哪里并不重要。 *** 是针对有问题的代码,而代码审查是为了帮助加速有效的代码。这是狙击手VS霰弹枪的问题。 Stack Overflow 是一个狙击手,具体问题。按照设定的标准,对于这个论坛来说,代码的改进太广泛了。因为它不适用于代码审查。这就是代码审查坚持代码有效的原因。 @PieterGeerkens 随时注册并在没有威胁的庄园中回答问题。尝试改变那里的气氛,而不是改变这里的规则。 您似乎在最后一天问了 3 次几乎相同的问题... VBA 代码需要很长时间才能执行 并且不响应 cmets,除非你说“谢谢,它工作得很好”或“如果这个 VBA 代码可以修改,从输入工作表运行”。请 - 回复 cmets,让我们觉得至少那时信息已被采纳。 【参考方案1】:

对 Excel 单元格的每次读取和写入都承载着巨大的包袱。通过将范围的数组复制到数组,然后将数组分配回范围,迭代复制到范围或从范围中复制的性能可以快 30 到 100 倍。

只是一张图片,但这里是一个示例,即使禁用屏幕更新后,使用数组复制而不是逐单元复制,性能也提高了 30 倍以上:

【讨论】:

【参考方案2】:

像这样加速 VBA 的第一件事就是使用屏幕更新

Sub LOOK()

Application.ScreenUpdating = False

Worksheets("Input").Unprotect ("ds12345")
Dim found As Range
Set found = Sheets("Records").Columns("D").Find(What:=ActiveSheet.Cells(3, 13).Value, SearchDirection:=xlPrevious, LookIn:=xlValues, LookAt:=xlWhole)
If found Is Nothing Then
    MsgBox "Not found"
Else
    MsgBox "Found on row " & found.Row
End If

Dim iRow As Integer

iRow = InputBox("Which row?")

Worksheets("Input").Cells(2, 1).Value = Worksheets("Records").Cells(iRow, 2).Value
Worksheets("Input").Cells(4, 2).Value = Worksheets("Records").Cells(iRow, 3).Value
Worksheets("Input").Cells(3, 13).Value = Worksheets("Records").Cells(iRow, 4).Value
Worksheets("Input").Cells(4, 13).Value = Worksheets("Records").Cells(iRow, 5).Value
Worksheets("Input").Cells(10, 3).Value = Worksheets("Records").Cells(iRow, 6).Value
Worksheets("Input").Cells(3, 16).Value = Worksheets("Records").Cells(iRow, 7).Value
Worksheets("Input").Cells(9, 7).Value = Worksheets("Records").Cells(iRow, 9).Value
Worksheets("Input").Cells(12, 7).Value = Worksheets("Records").Cells(iRow, 10).Value
Worksheets("Input").Cells(13, 7).Value = Worksheets("Records").Cells(iRow, 11).Value
Worksheets("Input").Cells(9, 14).Value = Worksheets("Records").Cells(iRow, 12).Value
Worksheets("Input").Cells(94, 5).Value = Worksheets("Records").Cells(iRow, 13).Value
Worksheets("Input").Cells(95, 5).Value = Worksheets("Records").Cells(iRow, 14).Value
Worksheets("Input").Cells(97, 2).Value = Worksheets("Records").Cells(iRow, 15).Value
Worksheets("Input").Cells(20, 1).Value = Worksheets("Records").Cells(iRow, 16).Value
Worksheets("Input").Cells(21, 1).Value = Worksheets("Records").Cells(iRow, 17).Value
Worksheets("Input").Cells(22, 1).Value = Worksheets("Records").Cells(iRow, 18).Value
Worksheets("Input").Cells(23, 1).Value = Worksheets("Records").Cells(iRow, 19).Value
Worksheets("Input").Cells(24, 1).Value = Worksheets("Records").Cells(iRow, 20).Value
Worksheets("Input").Cells(25, 1).Value = Worksheets("Records").Cells(iRow, 21).Value
Worksheets("Input").Cells(26, 1).Value = Worksheets("Records").Cells(iRow, 22).Value
Worksheets("Input").Cells(27, 1).Value = Worksheets("Records").Cells(iRow, 23).Value
Worksheets("Input").Cells(28, 1).Value = Worksheets("Records").Cells(iRow, 24).Value
Worksheets("Input").Cells(29, 1).Value = Worksheets("Records").Cells(iRow, 25).Value
Worksheets("Input").Cells(30, 1).Value = Worksheets("Records").Cells(iRow, 26).Value
Worksheets("Input").Cells(31, 1).Value = Worksheets("Records").Cells(iRow, 27).Value
Worksheets("Input").Cells(32, 1).Value = Worksheets("Records").Cells(iRow, 28).Value
Worksheets("Input").Cells(33, 1).Value = Worksheets("Records").Cells(iRow, 29).Value
Worksheets("Input").Cells(34, 1).Value = Worksheets("Records").Cells(iRow, 30).Value
Worksheets("Input").Cells(20, 11).Value = Worksheets("Records").Cells(iRow, 31).Value
Worksheets("Input").Cells(21, 11).Value = Worksheets("Records").Cells(iRow, 32).Value
Worksheets("Input").Cells(22, 11).Value = Worksheets("Records").Cells(iRow, 33).Value
Worksheets("Input").Cells(23, 11).Value = Worksheets("Records").Cells(iRow, 34).Value
Worksheets("Input").Cells(24, 11).Value = Worksheets("Records").Cells(iRow, 35).Value
Worksheets("Input").Cells(42, 11).Value = Worksheets("Records").Cells(iRow, 37).Value
Worksheets("Input").Cells(43, 11).Value = Worksheets("Records").Cells(iRow, 38).Value
Worksheets("Input").Cells(44, 11).Value = Worksheets("Records").Cells(iRow, 39).Value
Worksheets("Input").Cells(45, 11).Value = Worksheets("Records").Cells(iRow, 40).Value
Worksheets("Input").Cells(46, 11).Value = Worksheets("Records").Cells(iRow, 41).Value
Worksheets("Input").Cells(47, 11).Value = Worksheets("Records").Cells(iRow, 42).Value
Worksheets("Input").Cells(48, 11).Value = Worksheets("Records").Cells(iRow, 43).Value
Worksheets("Input").Cells(58, 5).Value = Worksheets("Records").Cells(iRow, 44).Value
Worksheets("Input").Cells(59, 5).Value = Worksheets("Records").Cells(iRow, 45).Value
Worksheets("Input").Cells(60, 5).Value = Worksheets("Records").Cells(iRow, 46).Value
Worksheets("Input").Cells(58, 8).Value = Worksheets("Records").Cells(iRow, 47).Value
Worksheets("Input").Cells(59, 8).Value = Worksheets("Records").Cells(iRow, 48).Value
Worksheets("Input").Cells(60, 8).Value = Worksheets("Records").Cells(iRow, 49).Value
Worksheets("Input").Cells(58, 11).Value = Worksheets("Records").Cells(iRow, 50).Value
Worksheets("Input").Cells(59, 11).Value = Worksheets("Records").Cells(iRow, 51).Value
Worksheets("Input").Cells(60, 11).Value = Worksheets("Records").Cells(iRow, 52).Value
Worksheets("Input").Cells(58, 14).Value = Worksheets("Records").Cells(iRow, 53).Value
Worksheets("Input").Cells(59, 14).Value = Worksheets("Records").Cells(iRow, 54).Value
Worksheets("Input").Cells(60, 14).Value = Worksheets("Records").Cells(iRow, 55).Value
Worksheets("Input").Cells(65, 18).Value = Worksheets("Records").Cells(iRow, 56).Value
Worksheets("Input").Cells(104, 18).Value = Worksheets("Records").Cells(iRow, 76).Value

If [l8] = [n9] Then
    [K8] = "YES"
    End If
If [l9] = [n9] Then
    [K9] = "YES"
    End If
If [l10] = [n9] Then
    [K10] = "YES"
    End If
If [l12] = [n9] Then
    [K12] = "YES"
    End If
If [l13] = [n9] Then
    [K13] = "YES"
    End If
If [l14] = [n9] Then
    [K14] = "YES"
    End If
If [l15] = [n9] Then
    [K15] = "YES"
    End If
If [l16] = [n9] Then
    [K16] = "YES"
    End If
If [l17] = [n9] Then
    [K17] = "YES"
End If

Worksheets("Input").Protect ("ds12345")

Application.ScreenUpdating = True

End Sub

为了代码的简洁性,当您有大量递增的值时,我会进行二次循环,例如:

Worksheets("Input").Cells(42, 11).Value = Worksheets("Records").Cells(iRow, 37).Value
Worksheets("Input").Cells(43, 11).Value = Worksheets("Records").Cells(iRow, 38).Value
Worksheets("Input").Cells(44, 11).Value = Worksheets("Records").Cells(iRow, 39).Value 
Worksheets("Input").Cells(45, 11).Value = Worksheets("Records").Cells(iRow, 40).Value
Worksheets("Input").Cells(46, 11).Value = Worksheets("Records").Cells(iRow, 41).Value
Worksheets("Input").Cells(47, 11).Value = Worksheets("Records").Cells(iRow, 42).Value
Worksheets("Input").Cells(48, 11).Value = Worksheets("Records").Cells(iRow, 43).Value

我们没有数据,因此很难说是否有更有效的选择方式,但我想在大多数情况下使用复制和粘贴特殊值会更快。

【讨论】:

以上是关于VBA代码在excel中执行需要很长时间的主要内容,如果未能解决你的问题,请参考以下文章

代码运行时未调用 Excel VBA 功能区 getEnabled

在使用 apache poi 从扩展名为 xlsx 的 Excel 文件中读取数据时,需要很长时间

Excel 加载项需要很长时间才能保存

Excel VBA脚本优化——遍历算法——解决这个难题?

Excel 等待很长时间才能开始保存文档

查询需要很长时间才能执行 laravel