Consulting

Results 1 to 6 of 6

Thread: Anyone have any idea on this code to speed up macros

  1. #1
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location

    Anyone have any idea on this code to speed up macros

    application.Visible = False 'Run Code Application.Visible = True

    can this code above be added to the below code

    Public CalcState As Long
    Public EventState As Boolean
    Public PageBreakState As Boolean
    
    
    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
    
    Second Piece of VBA Code
    Sub OptimizeCode_End()
    
    
    ActiveSheet.DisplayPageBreaks = PageBreakState
    Application.Calculation = CalcState
    Application.EnableEvents = EventState
    Application.ScreenUpdating = True

    Last edited by Paul_Hossler; 01-06-2019 at 01:51 PM.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Your 'code below' already has:
    Application.ScreenUpdating = False
    in the OptimizeCode_Begin sub, and:
    Application.ScreenUpdating = True
    in the OptimizeCode_End sub. What more do you expect?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    I thought the application.Visible= False was different from the Application.screenUpdating=False and thought by adding this would add extra speed To macros

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    My bad.

    Be careful with Application.Visible= False; if your code crashes or needs you to respond to a dialogue box, you'll be left with a hidden instance of Excel running in the background, with open workbooks and you'll need to use Task Manager to kill it, losing any unsaved work.

    In any event, Application.Visible= False is unlikely to make a material difference to the execution speed; it may actually slow it down slightly, as Windows has to hide Excel and repaint the screen with whatever you'd see if you minimised the Excel window,
    Last edited by macropod; 01-06-2019 at 09:11 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Mentor
    Joined
    Feb 2016
    Posts
    382
    Location
    Thank you, I’ll be sure not to use it!

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    It has its uses (e.g. when automating one application from another and you want the spawned application to run in the background).
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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