我在刪除列的代碼速度方面遇到問題。下面的代碼需要5秒鐘才能運行,如果我在運行之前保存文檔,那麼它只需要1秒。節省不是很多,但我有一臺體面的計算機,其他運行此表的用戶基本上都在瘦客戶端上,而且我有更多的代碼可以運行。另外,當工作簿完成時,我無法保存,因爲它被用作模板並在完成時保存爲新的工作簿。具有數據的行數是〜49,000,我已經嘗試過,沒有ClearContents
。Excel VBA刪除列速度慢,除非先保存文檔
有沒有什麼辦法可以加速而不必先保存文件?
Sub GenerateOperations()
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
.CutCopyMode = False
End With
Dim OpSheet As Worksheet
Set OpSheet = Sheet3
Application.Calculation = xlCalculationManual
OpSheet.Range("A:C,E:G,L:Q").ClearContents
OpSheet.Range("A:C,E:G,L:Q").Delete xlShiftToLeft
Application.Calculation = xlCalculationAutomatic
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
End With
End Sub
他爲什麼要關閉它?強烈建議關閉屏幕更新,以提高性能 –
這不會加快它 – Rdster
是關閉它加快了速度。根據我的經驗重新開啓它是不必要的,如果之後立即調用另一個子程序,則可能會減慢代碼。 – Nick