0

I have an application that has been running fine for several months. Today it suddenly takes 7 seconds to execute application.screenupdating = True.

Has anyone observed this strange behaviour before and if so, please point me in the right direction? I've spent many hours trying to find out what's causing this and researching, to no avail.

The problem only occurs right after the application has scanned across columns and automatically hiding and unhiding columns - as needed. On conclusion it calls Sub SettingsOn, which only has the following:

Sub SettingsOn ()
    With Application 
       .EnableEvents = True
       .ScreenUpdating = True ' this is the problem command
       .DisplayAlerts = True
    End With
End Sub

I have also tried switching around the sequence with no effect.

This is the debug.print output of before and after:

  • Screenupdate = False SettingsOn 1 11/03/2018 7:08:39 PM
  • UsedRange $A$1:$ABP$66 11/03/2018 7:08:39 PM
  • {this is where the .screenupdating = True executes}
  • Screenupdate = True SettingsOn 2 11/03/2018 7:08:46 PM

The delay is consistently around 7 to 8 seconds after completion the column manipulation. At all other times it executes instantly. As I said earlier, it has never been a problem until now, it was always instant.

It's very strange, I've not seen anything like this before.

Community
  • 1
  • 1
  • (ps: I have also tried enablevents = false and calculation = manual to no avail) – Ed Chivers Mar 11 '18 at 10:36
  • How well does your application run if you leave screenupdating on from the start (i.e never disable it in the first place)? Are you able to identify a slow-running section of code? – Chris Melville Mar 11 '18 at 10:42
  • Did you add any screen elements which now take time to re-draw or did the performance issue occur out of the blue with no changes to the sheet or whatsoever? – Storax Mar 11 '18 at 10:43
  • I would postulate that an external link, possibly to a web page, network resource or cloud source would be the issue. –  Mar 11 '18 at 11:11
  • Hi Chris, thank for your response. The app runs super fast, I've tuned it to nth degree. e.g. array handling and range updates for fast hide and unhide. I also have a test bed that I use to monitor chnages in code in the code. It run 12 views from the menu across all the columns and normally takes 1.8 seconds for 12 views. And in this app stopping screenupdating has a big performance gain. Thanks for the thought :) – Ed Chivers Mar 11 '18 at 11:25
  • Hi Storax, thanks for your response. Nothing new - it did have a large chart but sadly even deleting that makes no difference to the 7 seconds. Cheers – Ed Chivers Mar 11 '18 at 11:28
  • Hi Jeeped, thanks for your response. A good thought, I did have two URL hyperlinks and a link to a server. I just deleted them all but no difference. Thanks again. – Ed Chivers Mar 11 '18 at 11:35
  • Hi Storax, Sorry, it's late and I just remembered, I inserted some columns to the left of the sheet in the past 24 hours, in front of column A for room for timestamping and change logging. (for future merging of sheets) No code as yet, just the room in the sheet. I also added some code for copying a formula into a column (about 25 cells deep), but that's not executed in this scenario (and works fine when it is used). – Ed Chivers Mar 11 '18 at 11:49
  • Hi ashleedawg, thanks for your very comprehensive response, much appreciated. :) I replaced my SettingsOn sub with yours and same result. EnableEvents = 0.000 sec ScreenUpdating = 6.961 sec DisplayAlerts = 0.000 sec – Ed Chivers Mar 11 '18 at 20:07
  • Hi ashleedawg, I also tried the getmemusage Mem usage: 346456 EnableEvents = @ 12/03/2018 10:59:03 AM Timer: 0.000 sec ScreenUpdating = @ 12/03/2018 10:59:10 AM Timer: 7.227 sec DisplayAlerts = @ 12/03/2018 10:59:10 AM Timer: 0.000 sec Mem usage: 346436 I have also trapped all events but nothing is being called. Cheers – Ed Chivers Mar 12 '18 at 01:03
  • Folks, Many thanks for your responses, much appreciated. Unfortunately it's still no good and there's no clear indication as to why. I'm now certain it's some kind of internal Excel corruption. I have now reverted to the last good copy, copied over all the latest vba code and aligned them and it's working properly. Thanks for your efforts to help. Cheers – Ed Chivers Mar 12 '18 at 01:34

1 Answers1

1

I'm not convinced that ScreenUpdating = True is the culprit.

EnableEvents is more likely to cause a delay of more than a split-second. Which events are you using?

To confirm your diagnosis, temporarily replace your SettingsOn procedure with the one below, then run your app like you were before, copy the result from the Immediate Window, and then edit your question to post the result. (If the Immediate Window is not showing the result when the code stops, hit Ctrl+G) :

Sub SettingsOn()
    Dim startTime As Single
    With Application
       startTime = Timer: .EnableEvents = True
       Debug.Print vbLf & vbLf & "EnableEvents =   " & Format(Timer - startTime, "0.000 sec")
       startTime = Timer: .ScreenUpdating = True
       Debug.Print "ScreenUpdating = " & Format(Timer - startTime, "0.000 sec")
       startTime = Timer: .DisplayAlerts = True
       Debug.Print "DisplayAlerts =  " & Format(Timer - startTime, "0.000 sec") & vbLf
       MsgBox "After clicking OK, hit CTRL+G to open immediate window, then Copy the last 3 lines."
       Stop
    End With
End Sub

More to consider:

  • If there are queued events when EnableEvents is re-enabled, it could take some time to catch up.

  • Another possible affecting factor is memory usage. If a coding bug like a memory leak is (or was present), that can slow things considerably, among other unpredictable behaviours. Have you rebooted recently? Are you working with any object that, perhaps, aern't getting .Closed properly?

Seemingly insignificant bugs could cause ScreenUpdating or EnableEvents to behave differently.

  • If you add a .Repaint at the beginning of the test procedure, do the results change?

  • What prompted you to noticed the 7-second delay? ie., How often are you disabling & re-enabling?

Other ways to increase speed:

  • Disable automatic calculation by setting Application.Calculation = xlCalculationManual (and back to xlCalculationAutomatic afterwards)

  • Disable status bar updates with Application.DisplayStatusBar = False

  • Excel has to recalculate page breaks with each worksheet change, unless it's disabled with Activesheet.DisplayPageBreaks = False

  • Prevent Pivot Table updates: ActiveSheet.PivotTables(“PivotTable1”).ManualUpdate = True

  • If your code is copying-and-pastng cells, that takes a considerable amount if time (including recalculation), especially if you're using code like:

    Range("A1").Select Selection.Copy Range("A2").Select ActiveSheet.Paste

    Speed it up by skipping the clipboard: (apparently 25x faster)

    Range("A1").Copy Destination:=Range("A2")

More ideas here.


This function could be used to check memory usage during code execution: (Source)

Function GetMemUsage()
    ' Returns the current Excel.Application Working Memory usage in KB
    Dim objSWbemServices: Set objSWbemServices = GetObject("winmgmts:")
    GetMemUsage = objSWbemServices.Get("Win32_Process.Handle='" & GetCurrentProcessId & "'").WorkingSetSize / 1024
    Set objSWbemServices = Nothing
End Function

Numerous other metrics can also be verified by replacing WorkingSetSize. Complete list here.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105