The code below runs fast and as expected when used in a workbook with a single worksheet.
The code below takes forever to execute when used in a workbook that contains 50 of these worksheets all with this same code.
It appears that the code executes on all worksheets when activated on the active worksheet.
I only want the code to execute on the active worksheet.
What changes do I need to make?
Private Sub Worksheet_Calculate()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
Static oldval
If ws.Range("$CA$17").Value <> oldval Then
oldval = ws.Range("$CA$17").Value
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
ws.Unprotect "2025"
Dim C As Range
ws.Range("A:AFS").EntireColumn.Hidden = False
For Each C In ws.Range("A391:AFS391").Cells
If C.Value = 0 Then
C.EntireColumn.Hidden = True
End If
Next C
ws.Range("BB1").AutoFilter Field:=1, Criteria1:=1, Operator:=xlOr, Criteria2:=2
ws.Protect Password:="2025", DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables _
:=True
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True
End If
End Sub
The code below takes forever to execute when used in a workbook that contains 50 of these worksheets all with this same code.
It appears that the code executes on all worksheets when activated on the active worksheet.
I only want the code to execute on the active worksheet.
What changes do I need to make?
Private Sub Worksheet_Calculate()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
Static oldval
If ws.Range("$CA$17").Value <> oldval Then
oldval = ws.Range("$CA$17").Value
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
ActiveSheet.DisplayPageBreaks = False
ws.Unprotect "2025"
Dim C As Range
ws.Range("A:AFS").EntireColumn.Hidden = False
For Each C In ws.Range("A391:AFS391").Cells
If C.Value = 0 Then
C.EntireColumn.Hidden = True
End If
Next C
ws.Range("BB1").AutoFilter Field:=1, Criteria1:=1, Operator:=xlOr, Criteria2:=2
ws.Protect Password:="2025", DrawingObjects:=False, Contents:=True, Scenarios:= _
False, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowInsertingColumns:=True, AllowInsertingRows _
:=True, AllowSorting:=True, AllowFiltering:=True, AllowUsingPivotTables _
:=True
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
ActiveSheet.DisplayPageBreaks = True
End If
End Sub