B
Breck
I recently added 1 new line of code to the following code I have been
running for a year. Previous to adding the the line of code
"PageSetup.CenterHeader = Range("A1").Value" I would click on a sheet
and there would be a slight almost unnoticable pause while the code
executed. After adding the above code there is now a 5 to 8 second
pause after clicking on the sheet. This pause is the same whether Calc
is on or off.
Is it just the nature of that new line of code or is there a way of
tweaking my code so it executes faster. I have 32 sheets with the same
code for each sheet. The entire Excel 2007 spreadsheet is 7MB
compress. It it was saved in Excel 2003 the file size would approach
20MB so it is a very large file.
What I'm trying to achieve is to make sure that the "crosses at" value
on 9 different charts is updated with the most current value that is
on the same sheet. This is the part that is working fine. I wanted to
update each of the sheets Header with data that is entered in another
location also. That is the additional code that causing the 5-8 second
delay. I also tried code that would update the header of every sheet
after the desired text is entered on a sheet but that code took 5-10
minuted to run so I tried the individual sheet approach hoping that
would run faster.
Private Sub worksheet_activate()
Application.ScreenUpdating = False
'In case sheets are grouped
Sheet4.Select
On Error GoTo Ungroup
'To Unprotect this sheet if protected
If Me.ProtectContents = True Then
ActiveSheet.Select
ActiveSheet.Unprotect
Else
End If
'This updates the sheet print header
PageSetup.CenterHeader = Range("A1").Value <======== This is the
new line of code that slows everything down
'To Update the Charts CrossesAt value from setup sheet that are on
the sheet
With ActiveSheet
If .ChartObjects("Chart 1").Chart.Axes(xlValue).CrossesAt
<> .Range("J20") Then
.ChartObjects("Chart 1").Chart.Axes(xlValue).CrossesAt
= .Range("J20")
End If
If .ChartObjects("Chart 2").Chart.Axes(xlValue).CrossesAt
<> .Range("K20") Then
.ChartObjects("Chart 2").Chart.Axes(xlValue).CrossesAt
= .Range("K20")
End If
If .ChartObjects("Chart 3").Chart.Axes(xlValue).CrossesAt
<> .Range("L20") Then
.ChartObjects("Chart 3").Chart.Axes(xlValue).CrossesAt
= .Range("L20")
End If
If .ChartObjects("Chart 4").Chart.Axes(xlValue).CrossesAt
<> .Range("Q20") Then
.ChartObjects("Chart 4").Chart.Axes(xlValue).CrossesAt
= .Range("Q20")
End If
If .ChartObjects("Chart 5").Chart.Axes(xlValue).CrossesAt
<> .Range("R20") Then
.ChartObjects("Chart 5").Chart.Axes(xlValue).CrossesAt
= .Range("R20")
End If
If .ChartObjects("Chart 6").Chart.Axes(xlValue).CrossesAt
<> .Range("S20") Then
.ChartObjects("Chart 6").Chart.Axes(xlValue).CrossesAt
= .Range("S20")
End If
If .ChartObjects("Chart 7").Chart.Axes(xlValue).CrossesAt
<> .Range("T20") Then
.ChartObjects("Chart 7").Chart.Axes(xlValue).CrossesAt
= .Range("T20")
End If
If .ChartObjects("Chart 8").Chart.Axes(xlValue).CrossesAt
<> .Range("AA65") Then
.ChartObjects("Chart 8").Chart.Axes(xlValue).CrossesAt
= .Range("AA65")
End If
If .ChartObjects("Chart 9").Chart.Axes(xlValue).CrossesAt
<> .Range("AB65") Then
.ChartObjects("Chart 9").Chart.Axes(xlValue).CrossesAt
= .Range("AB65")
End If
End With
Range("AI1:AI262").AutoFilter Field:=1, Criteria1:="<>0"
If Me.ProtectContents = False Then
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True, AllowFormattingColumns:=True, AllowFiltering:=True
'MsgBox "Protected"
Else
ActiveSheet.Unprotect
'MsgBox "Un Protected"
End If
'Also see below
'Me.ProtectDrawingObjects
On Error GoTo 0
'The macro is done. Use Exit sub, otherwise the macro
'execution WILL continue into the error handler
Exit Sub
Ungroup:
MsgBox "Please ungroup sheets"
End Sub
running for a year. Previous to adding the the line of code
"PageSetup.CenterHeader = Range("A1").Value" I would click on a sheet
and there would be a slight almost unnoticable pause while the code
executed. After adding the above code there is now a 5 to 8 second
pause after clicking on the sheet. This pause is the same whether Calc
is on or off.
Is it just the nature of that new line of code or is there a way of
tweaking my code so it executes faster. I have 32 sheets with the same
code for each sheet. The entire Excel 2007 spreadsheet is 7MB
compress. It it was saved in Excel 2003 the file size would approach
20MB so it is a very large file.
What I'm trying to achieve is to make sure that the "crosses at" value
on 9 different charts is updated with the most current value that is
on the same sheet. This is the part that is working fine. I wanted to
update each of the sheets Header with data that is entered in another
location also. That is the additional code that causing the 5-8 second
delay. I also tried code that would update the header of every sheet
after the desired text is entered on a sheet but that code took 5-10
minuted to run so I tried the individual sheet approach hoping that
would run faster.
Private Sub worksheet_activate()
Application.ScreenUpdating = False
'In case sheets are grouped
Sheet4.Select
On Error GoTo Ungroup
'To Unprotect this sheet if protected
If Me.ProtectContents = True Then
ActiveSheet.Select
ActiveSheet.Unprotect
Else
End If
'This updates the sheet print header
PageSetup.CenterHeader = Range("A1").Value <======== This is the
new line of code that slows everything down
'To Update the Charts CrossesAt value from setup sheet that are on
the sheet
With ActiveSheet
If .ChartObjects("Chart 1").Chart.Axes(xlValue).CrossesAt
<> .Range("J20") Then
.ChartObjects("Chart 1").Chart.Axes(xlValue).CrossesAt
= .Range("J20")
End If
If .ChartObjects("Chart 2").Chart.Axes(xlValue).CrossesAt
<> .Range("K20") Then
.ChartObjects("Chart 2").Chart.Axes(xlValue).CrossesAt
= .Range("K20")
End If
If .ChartObjects("Chart 3").Chart.Axes(xlValue).CrossesAt
<> .Range("L20") Then
.ChartObjects("Chart 3").Chart.Axes(xlValue).CrossesAt
= .Range("L20")
End If
If .ChartObjects("Chart 4").Chart.Axes(xlValue).CrossesAt
<> .Range("Q20") Then
.ChartObjects("Chart 4").Chart.Axes(xlValue).CrossesAt
= .Range("Q20")
End If
If .ChartObjects("Chart 5").Chart.Axes(xlValue).CrossesAt
<> .Range("R20") Then
.ChartObjects("Chart 5").Chart.Axes(xlValue).CrossesAt
= .Range("R20")
End If
If .ChartObjects("Chart 6").Chart.Axes(xlValue).CrossesAt
<> .Range("S20") Then
.ChartObjects("Chart 6").Chart.Axes(xlValue).CrossesAt
= .Range("S20")
End If
If .ChartObjects("Chart 7").Chart.Axes(xlValue).CrossesAt
<> .Range("T20") Then
.ChartObjects("Chart 7").Chart.Axes(xlValue).CrossesAt
= .Range("T20")
End If
If .ChartObjects("Chart 8").Chart.Axes(xlValue).CrossesAt
<> .Range("AA65") Then
.ChartObjects("Chart 8").Chart.Axes(xlValue).CrossesAt
= .Range("AA65")
End If
If .ChartObjects("Chart 9").Chart.Axes(xlValue).CrossesAt
<> .Range("AB65") Then
.ChartObjects("Chart 9").Chart.Axes(xlValue).CrossesAt
= .Range("AB65")
End If
End With
Range("AI1:AI262").AutoFilter Field:=1, Criteria1:="<>0"
If Me.ProtectContents = False Then
ActiveSheet.Protect DrawingObjects:=True, Contents:=True,
Scenarios:=True, AllowFormattingColumns:=True, AllowFiltering:=True
'MsgBox "Protected"
Else
ActiveSheet.Unprotect
'MsgBox "Un Protected"
End If
'Also see below
'Me.ProtectDrawingObjects
On Error GoTo 0
'The macro is done. Use Exit sub, otherwise the macro
'execution WILL continue into the error handler
Exit Sub
Ungroup:
MsgBox "Please ungroup sheets"
End Sub