2017-02-23 84 views
1

您是否知道我可以如何加快運行以下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 
+2

什麼是ClearDataEntry?和一個小的語法方面是你不必選擇行,你可以做這個'行(「2」)。EntireRow.Hidden =假' – Zerk

+4

這是一個問題http://codereview.stackexchange.com/ –

+0

刪除'ButtonNames'中的每個ButtonName行和'Next ButtonName'行。你也可以刪除'ButtonNames = Array(「ButtonUpdateExisting」,「ButtonAddNew」,「ClearForm」)''行。另外,'ClearDataEntry'調用另一個我們看不到的子。因此,請在您的帖子中包含此代碼,以便我們可以看到問題是否在該程序中。 – Ralph

回答

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