Application.ScreenUpdating = False not working properly

gers1978

Board Regular
Joined
Sep 9, 2014
Messages
74
I call a function right at the start of my code that looks like this:

Code:
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

At the end of my code I call:

Code:
Sub OptimizeCode_End()

ActiveSheet.DisplayPageBreaks = PageBreakState
Application.Calculation = CalcState
Application.EnableEvents = EventState
Application.ScreenUpdating = True

End Sub

In one of the subs that runs in between, I have the following code:

Code:
If .Worksheets(intCount).Name = "Invoice" Then
  Worksheets("Invoice").Delete
    .Worksheets.Add
  With ActiveSheet
    .Name = "Invoice"
  End With
  booFound = True
  Exit For
End If

Yet for some reason, at the line:

Code:
Worksheets("Invoice").Delete

I still get the warning about deleting a sheet.

If I change that block above to:

Code:
If .Worksheets(intCount).Name = "Invoice" Then
  Application.DisplayAlerts = False
  Worksheets("Invoice").Delete
  Application.DisplayAlerts = True
    .Worksheets.Add
  With ActiveSheet
    .Name = "Invoice"
  End With
  booFound = True
  Exit For
End If

It works fine. But my question is why do I need the "Application.DisplayAlerts = False" immediately before the .Delete statement? Surely alerts should already be set to False due to the initial sub setting it that way?

Also posted here:

Application.ScreenUpdating = False not working properly
 
Last edited:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
But my question is why do I need the "Application.DisplayAlerts = False" immediately before the .Delete statement? Surely alerts should already be set to False due to the initial sub setting it that way?
You don't have Application.DisplayAlerts set to false in that initial sub. It isn't mentioned.
 
Upvote 0
You don't have Application.DisplayAlerts set to false in that initial sub. It isn't mentioned.

Oops, but even after I add it:

Code:
    ActiveSheet.DisplayPageBreaks = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
    Application.DisplayStatusBar = False
    Application.ScreenUpdating = False

And run that sub at the start of my code, I STILL get the "delete sheet" warning at:

Code:
Worksheets("Invoice").Delete

unless I do Application.DisplayAlerts = False immediately before that line
 
Upvote 0
Works fine for me making that change.
 
Upvote 0

Forum statistics

Threads
1,214,869
Messages
6,122,015
Members
449,060
Latest member
LinusJE

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top