I have two simple scripts that runs progressively slower each time I run them. One adds a row, another removes a row. Besides that, all that's done is some format copying to ensure the table still looks pretty.
Here's the issue: I found if I add a row, remove it, then save the xlsm, the file size increases. Each time I do this, it seems to increase the runtime to the point where the spreadsheet locks up for a few seconds.
For context: calcCOPbottomRow
is a row in the excel spreadsheet.
All other named cells are single cell values.
Here they are:
Sub Add_System()
Call OptimizeCode_Begin
'Select bottom row of table and insert a new row
Range("calcCOPbottomRow").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Dim formatRange As Range
Dim rowNum As Long
'Clean up formatting
rowNum = Range("calcCOPbottomRow").Row - 3
Set formatRange = Range(CStr(rowNum) & ":" & CStr(rowNum + 1))
formatRange.Copy
Rows(CStr(rowNum + 1) & ":" & CStr(rowNum + 2)).Select
ActiveSheet.PasteSpecial Format:=4, Link:=1, DisplayAsIcon:=False, _
IconFileName:=False
'Select new row
Range("calcCOPTableEnd").Offset(-1, 0).Select
Call OptimizeCode_End
End Sub
Sub Remove_System()
If Range("nSystems") <= 1 Then
MsgBox "Cannot remove final row of COP Calculator Table"
Exit Sub
End If
Call OptimizeCode_Begin
Dim formatRange As Range
Dim rowNum As Long
'Clean up formatting
rowNum = Range("calcCOPbottomRow").Row - 2
Set formatRange = Range(CStr(rowNum) & ":" & CStr(rowNum + 1))
formatRange.Copy
Rows(CStr(rowNum - 1) & ":" & CStr(rowNum)).Select
ActiveSheet.PasteSpecial Format:=4, Link:=1, DisplayAsIcon:=False, _
IconFileName:=False
'Delete system row
Range("calcCOPbottomRow").Offset(-1, 0).Select
Selection.Delete Shift:=xlUp
'Select new row
Range("calcCOPTableEnd").Offset(-1, 0).Select
Call OptimizeCode_End
End Sub
Is there something in that code I'm not considering that causes this progressive slowdown? For the record, the OptimizeCode_End and OptimizeCode_Start had no impact on this, but if you're curious they're here:
Sub OptimizeCode_Begin()
Application.ScreenUpdating = False
EventState = Application.EnableEvents
Application.EnableEvents = False
CalcState = Application.Calculation
Application.Calculation = xlCalculationManual
PageBreakState = ActiveSheet.DisplayPageBreaks
ActiveSheet.DisplayPageBreaks = False
End Sub
Sub OptimizeCode_End()
ActiveSheet.DisplayPageBreaks = PageBreakState
Application.Calculation = CalcState
Application.EnableEvents = EventState
Application.ScreenUpdating = True
End Sub
Any tips would be appreciated-- I'm pretty new to this stuff.
Thanks!