+ Reply to Thread
Results 1 to 13 of 13

A function to replace all the application.screenupdating references

Hybrid View

  1. #1
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    A function to replace all the application.screenupdating references

    Is there a method which could replace all of the numerous places in code in-which the code below is used? Maybe some type of function which is called instead of repeating the same code over and over or maybe this is the best method.

        With Application
            .ScreenUpdating = False
            .EnableEvents = False
            .DisplayAlerts = False
            .Calculation = xlCalculationManual
        End With
    Edit: Sorry posted in the wrong forum
    Last edited by jeffreybrown; 03-12-2011 at 05:30 PM.
    HTH
    Regards, Jeff

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: A function to replace all the appliction.screenupdating references

    Jeff,

    You should be able to create a standard module and in it create a Public Sub with that code inside. You can then call that Sub from any worksheet within the workbook.

  3. #3
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: A function to replace all the appliction.screenupdating references

    Hi Paul,

    Since I am still learning the ropes of VBA, is this a good method or should you stick within each procedure or is it just preference?

  4. #4
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: A function to replace all the appliction.screenupdating references

    Generally if you repeat steps multiple times it is cleaner to have one instance of it, then call it when necessary. It does save a little memory. Normally it would be used for repetitive functions or more drawn-out code, but it will work in this instance just as in any other.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: A function to replace all the appliction.screenupdating references

    Maybe you could use somthing like this, Jeff. It maintains a stack of states so subs can push their desired state and then pop to return to the state when they were called. You can skip arguments that you don't care about.

    Minimally tested.

    Option Explicit
    
    Public Enum AppMode
        Pop
        Reset
        Push
    End Enum
    
    Sub demo()
        ' set calculation to manual and screen updating to false
        AppOnOff Push, iCalc:=xlCalculationManual, tsScreen:=vbFalse
        ' do stuff
        AppOnOff Pop
    End Sub
    
    Sub AppOnOff(iMode As AppMode, _
                 Optional iCalc As XlCalculation = 0, _
                 Optional tsAlerts As VbTriState = vbUseDefault, _
                 Optional tsEvents As VbTriState = vbUseDefault, _
                 Optional tsScreen As VbTriState = vbUseDefault)
        ' iMode     Action
        '   -1      Pop to last state
        '    0      Reset state, everything on
        '    1      Push current state, set new states
    
        Static col      As Collection
        Dim vSta        As Variant
    
        If col Is Nothing Then Set col = New Collection
    
        With Application
            Select Case iMode
                Case Pop
                    If col.Count Then
                        vSta = col.Item(col.Count)
                        col.Remove col.Count
                        
                        .Calculation = vSta(0)
                        .DisplayAlerts = vSta(1)
                        .EnableEvents = vSta(2)
                        .ScreenUpdating = vSta(3)
                    End If
    
                Case Reset
                    Set col = New Collection
                    .Calculation = xlCalculationAutomatic
                    .DisplayAlerts = True
                    .EnableEvents = True
                    .ScreenUpdating = True
    
                Case Push
                    col.Add VBA.Array(.Calculation, .DisplayAlerts, .EnableEvents, .ScreenUpdating)
                    
                    If iCalc <> 0 Then .Calculation = iCalc
                    If tsAlerts <> vbUseDefault Then .DisplayAlerts = tsAlerts
                    If tsEvents <> vbUseDefault Then .EnableEvents = tsEvents
                    If tsScreen <> vbUseDefault Then .ScreenUpdating = tsScreen
            End Select
        End With
    End Sub
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: A function to replace all the appliction.screenupdating references

    Thanks Paul/shg,

    @shg...I couldn't quite get your code to cooperate but I will keep trying. In the meantime I have run across this...

    Option Explicit
        Public booScrnUpdt As Boolean
        Public booEnabEvnt As Boolean
        Public booDispAlrt As Boolean
    '    Public lngCalcMode As XlCalculation
    
    Sub DoingStuff()
        Call AppStateFalse
        Sheets("Sheet2").Range("A1").Copy Destination:=Sheets("Sheet2").Range("A2")
        Call AppStateRestore
    End Sub
    
    Sub AppStateFalse()
        With Application
            Let booScrnUpdt = .ScreenUpdating
            Let booEnabEvnt = .EnableEvents
            Let booDispAlrt = .DisplayAlerts
    '        Let lngCalcMode = .Calculation
            .ScreenUpdating = False
            .EnableEvents = False
            .DisplayAlerts = False
    '        .Calculation = xlCalculationManual
        End With
     End Sub
     
     Sub AppStateRestore()
        With Application
            .ScreenUpdating = booScrnUpdt
            .EnableEvents = booEnabEvnt
            .DisplayAlerts = booDispAlrt
    '        .Calculation = lngCalcMode
        End With
    End Sub
    So far with my testing I have not seen any problems but from your experience do you see this as a sound method?

    I got the idea from here

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: A function to replace all the appliction.screenupdating references

    col.Add VBA.Array(.Calculation, .DisplayAlerts, .EnableEvents, .ScreenUpdating)
    @shg, can you outline the use of VBA qualifier in the above ?

    I've seen Colin using the VBA qualifier of late and presume this is for good reason but have found little info. on it.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: A function to replace all the appliction.screenupdating references

    @ DO: VBA.Array always generates a zero-based array, even if the user has Option Base 1.

    @ Jeffrey: The potential problem is that (a) AppStateFalse always sets each of the properties "off" when that may not be exactly what you want, and (b) you must alternately call AppStateFalse and AppStateRestore or else you lose the state. Mine was intended to solve the case where nested subs may want to set their environment during execution and then restore it on return to the calling routine.

    Sub bob()
        AppOnOff Push, iCalc:=xlCalculationManual, tsScreen:=vbFalse
        ' do stuff
        call joe
        AppOnOff Pop
    End Sub
    
    Sub joe()
        AppOnOff Push, ...
        ' do stuff
        AppOnOff Pop
    end sub
    What problems are you having? As I said, it got only a two-minute test from me.
    Last edited by shg; 03-12-2011 at 05:13 PM.

  9. #9
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: A function to replace all the application.screenupdating references

    Hi again shg,

    No matter what combination I run for the AppOnOff Push, I get the MsgBox which says "Oops!". Any ideas why, but in the end everything is restored to the previous state.

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: A function to replace all the application.screenupdating references

    I trust you mean for Pop.

    Sorry, corrected in prior post.
    Last edited by shg; 03-12-2011 at 07:31 PM.

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: A function to replace all the application.screenupdating references

    Works grand

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1