加快VBA代码

时间:2017-02-23 11:14:28

标签: excel vba excel-vba

您知道如何加快以下VBA代码的运行速度吗?我尝试过在线浏览,但可能还有其他我在这里缺少的东西。这是一个简单的脚本,我很惊讶它需要一些时间来运行它。感谢

Sub ExistingRecord()

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.EnableEvents = False

    Dim ButtonName As Variant
    Dim ButtonNames As Variant

    ButtonNames = Array("ButtonUpdateExisting", "ButtonAddNew", "ClearForm")
        Rows("2").Select
        Selection.EntireRow.Hidden = False
        Rows("3:22").Select
        Selection.EntireRow.Hidden = True

        Range("D2").Select
          For Each ButtonName In ButtonNames
          ActiveSheet.Buttons("ButtonUpdateExisting").Visible = True
          ActiveSheet.Buttons("ButtonAddNew").Visible = False
          ActiveSheet.Buttons("ClearForm").Visible = True
        Next ButtonName

    ClearDataEntry

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.DisplayStatusBar = True
    Application.EnableEvents = True

End Sub

1 个答案:

答案 0 :(得分:2)

不看ClearDataEntry,很难看出这种情况正在放缓。但是上面的代码可以简化为以下代码,这应该更快:

Sub ExistingRecord()

    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.EnableEvents = False

    Rows("2").EntireRow.Hidden = False
    Rows("3:22").EntireRow.Hidden = True

    ActiveSheet.Buttons("ButtonUpdateExisting").Visible = True
    ActiveSheet.Buttons("ButtonAddNew").Visible = False
    ActiveSheet.Buttons("ClearForm").Visible = True

    ClearDataEntry

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Application.DisplayStatusBar = True
    Application.EnableEvents = True

End Sub