I came to work this morning and began work on the same file I left from yesterday. Now Excel slows way down whenever a macro is assigning values to cells or clearing them. It was lightning quick before I left yesterday, now it takes 2-3 seconds at a time to assign a value to a cell.
Right now, other files with similar macros run fine. This happened to another file a while back, so I reverted to a previous version of the file and it worked fine, while the slow file continued to work slow. I guess I can do that now, but in this case that would take a whole lot of work to bring that file to where I have this version. I was wondering if anyone knows what might be going on. Is this one of the things Excel files just do from time to time (like a kind of file corruption), or is there a fix?
I have provided the macro below, although it happens throughout the file whenever values are assigned to cells. I've marked the trouble areas by denoting <---SLOW HERE
.
I know this all sounds very vague, and I know I am giving very little to go on. Perhaps insufficient info, but this is all I have. There is no reason (that I can see) that this should be happening. I've even restarted the computer...just in case the problem was external to Excel. No change.
If you need more info I will do my best to expound if I can. Thanks for your help and understanding.
Example Macro:
Sub DeleteButton1_Click()
Call UnlockSettingsWorksheet
Sheet24.Range("C18:E18").Value = "" <---SLOW HERE
Dim i As Long
For i = 18 To 21
Sheet24.Range("C" & i & ":E" & i).Value = Sheet24.Range("C" & i + 1 & ":E" & i + 1).Value <---SLOW HERE
Next
Sheet24.Range("C22:E22").Value = "" <---SLOW HERE
Call LockSettingsWorksheet
End Sub
As mentioned in the comments, there might be a lot of factors contributing to this change:
any actions triggered in these events:
external links, and the external file(s) moved or deleted
If the file is corrupt, and it's feasible, try re-creating it from scratch, and run this function first
If it's not corrupt, one of the first things I'd try is to disable all Excel functionality before the macro:
Sub DeleteButton1_Click()
'UnlockSettingsWorksheet
FastWB '<--- Disables all Application and Worksheet level settings
With ThisWorkbook.Worksheets("Sheet24") 'Fully qualified worksheet
.Range("C18:E18").Value2 = vbNullString
Dim i As Long
For i = 18 To 21
.Range("C" & i & ":E" & i).Value2 = .Range("C" & (i + 1) & ":E" & (i + 1) ).Value2
Next
.Range("C22:E22").Value2 = vbNullString
End With
XlResetSettings '<--- Restores all Excel settings to defaults
'LockSettingsWorksheet
End Sub
.
Public Sub FastWB(Optional ByVal opt As Boolean = True)
With Application
.Calculation = IIf(opt, xlCalculationManual, xlCalculationAutomatic)
.DisplayAlerts = Not opt
.DisplayStatusBar = Not opt
.EnableAnimations = Not opt
.EnableEvents = Not opt
.ScreenUpdating = Not opt
End With
FastWS , opt
End Sub
Public Sub FastWS(Optional ByVal ws As Worksheet, Optional ByVal opt As Boolean = True)
If ws Is Nothing Then
For Each ws In Application.ThisWorkbook.Sheets
OptimiseWS ws, opt
Next
Else
OptimiseWS ws, opt
End If
End Sub
Public Sub OptimiseWS(ByVal ws As Worksheet, ByVal opt As Boolean)
With ws
.DisplayPageBreaks = False
.EnableCalculation = Not opt
.EnableFormatConditionsCalculation = Not opt
.EnablePivotTable = Not opt
End With
End Sub
Public Sub XlResetSettings() 'default Excel settings
With Application
.Calculation = xlCalculationAutomatic
.DisplayAlerts = True
.DisplayStatusBar = True
.EnableAnimations = False
.EnableEvents = True
.ScreenUpdating = True
Dim ws As Worksheet
For Each ws In Application.ThisWorkbook.Sheets
With ws
.DisplayPageBreaks = False
.EnableCalculation = True
.EnableFormatConditionsCalculation = True
.EnablePivotTable = True
End With
Next
End With
End Sub
Maybe this will eliminate some VBA causes
I started noticing Range.Value = ...
operations being significantly slow once upgraded to a 64 bit edition. Aside form the normal stuff like .ScreenUpdating
and .Calculation
, two things I've discovered significantly improve the speed are:
Change the cursor to xlWait
prior to your operation, and back to xlDefault
once done.
Public Sub MyMacro() On Error Goto Exception Application.Cursor = xlWait ' Do something here. Exception: Application.Cursor = xlDefault End Sub
Use .Value2
instead of .Value
Sheet1.Range("A1").Value2 = "The quick brown fox jumps over the lazy dog."
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.