Home » excel » excel – Separate subroutine for application methods?

excel – Separate subroutine for application methods?

Posted by: admin April 23, 2020 Leave a comment

Questions:

Right now I’m in the middle of writing a mildly complicated network of Excel workbooks that is almost entirely driven by VBA automation, so there are lots of macros being written.

I try and disable screenupdating, alerts, and events wherever possible. To save some time and headaches in tracking down where I may have enabled/disabled these, I’ve written a subroutine where the desired settings for each are passed in as arguments, so you can set up what you want enabled/disabled in one line of code (or at least that’s the idea anyway).

This is what that subroutine looks like:

Sub AppToggles(Optional ScrUpdating As Boolean = False, _
               Optional DispAlerts As Boolean = True, _
               Optional Events As Boolean = False)

With Application    
   .ScreenUpdating = ScrUpdating
   .DisplayAlerts = DispAlerts
   .EnableEvents = Events
End With    

End Sub

I call it from various other subroutines in the same module with Call AppToggles(True, True, True) (or whatever combination is desired.

Problem is, the subroutine doesn’t seem to do anything. It’s such a simple routine that stepping through it tells me nothing. Hovering over the keywords shows me that each switch is not being set to the arguments passed.

I could set up an If statement for each one I suppose, but I’m curious as to where I fell off the tracks 😛

How to&Answers:

Don’t Step through the code… Run it normally or by F5

Your routine works fine for me.

Sub TestMe()
Call AppToggles(False, False, False)
MsgBox "ScreenUpdating: " & Application.ScreenUpdating & vbCrLf & _
"DisplayAlerts: " & Application.DisplayAlerts & vbCrLf & _
"EnableEvents: " & Application.EnableEvents
Call AppToggles(True, True, True)
MsgBox "ScreenUpdating: " & Application.ScreenUpdating & vbCrLf & _
"DisplayAlerts: " & Application.DisplayAlerts & vbCrLf & _
"EnableEvents: " & Application.EnableEvents
Call AppToggles(True, True, True)
End Sub

Answer:

Since you stated that everything is automated, did you, by any chance, disable Application.EnableEvents? I know it’s sometimes set to False to prevent BeforeSave from launching when saving the workbook.

If that’s the case, setting it to True just before manipulating ScreenUpdating and DisplayAlerts should do the trick. You can revert back to False (or to the value provided by the Events parameter), if needed, after everything is set.