+ Reply to Thread
Results 1 to 13 of 13

Application ScreenUpdating and Error Handling

  1. #1
    Registered User
    Join Date
    07-31-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    30

    Application ScreenUpdating and Error Handling

    Hi,

    When using Application ScreenUpdating = False and then turning it back on with Application ScreenUpdating = True where should the True line of code go when using error handling. Does it go right before End Sub as in the example below?

    Example

    Sub example ()

    On Error GoTo ErrMsg

    Application ScreenUpdating = False
    'Code here

    Exit Sub

    ErrMsg:
    MsgBox "Got an error."

    Application ScreenUpdating = True

    End Sub


    Thanks for taking the time to review.
    Karen

  2. #2
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Application ScreenUpdating and Error Handling

    Application ScreenUpdating = True is redundant. Is done automatically when the procedure is completed.
    Messages have been translated from Dutch to English by means of google translate.

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Application ScreenUpdating and Error Handling


    Hi !

    Vraag, it should be but it's not always the case …

    Karen, you must set ScreenUpdating to True between ErrMsg and the MsgBox codeline and just before the Exit Sub codeline …

  4. #4
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: Application ScreenUpdating and Error Handling

    @Vraag, from experience I have found it something of a myth.
    It may be the case that on completion of a macro screen updating returns to true, however I have experienced on many occasions over the years this is not always the case when working with a UserForm environment, this is also supported in the link below.
    Back to the OP question, place it both immediately before your 'Exit Sub' command and imediately after 'ErrMsg:' to ensure it is read in each scenario.
    torachan.


  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Application ScreenUpdating and Error Handling


    As I have also experimented the case without using any UserForm …

  6. #6
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Application ScreenUpdating and Error Handling

    A little weird comparison.

    Please Login or Register  to view this content.
    The whole procedure is only completed after the last end sub. If you then start something new, it will be set to true again.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    07-31-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    30

    Re: Application ScreenUpdating and Error Handling

    That was my thinking to be sure both scenarios are covered. Thanks

  8. #8
    Registered User
    Join Date
    07-31-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    30

    Re: Application ScreenUpdating and Error Handling

    Thanks! Appreciate the response.

  9. #9
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,539

    Re: Application ScreenUpdating and Error Handling

    When using error handling using labels, it is definitely better to use two labels as in the example below. Then we clean up what we messed up in only one place (after the ExitSub label).
    Please Login or Register  to view this content.
    Artik

  10. #10
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,539

    Re: Application ScreenUpdating and Error Handling

    Vraag, Torachan (and Marc L) is right. There are situations when the screen is not refreshed after the code is finished, even though ScreenUpadting has been automatically reset to True (without our intervention). If you don't want to force a screen refresh at the end of the macro, always check if you can see what you expected.

    Artik

  11. #11
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Application ScreenUpdating and Error Handling

    On error is unnecessary in almost all cases.

  12. #12
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: Application ScreenUpdating and Error Handling

    A easy illustration on a userform cycle through the four command buttons twice, the screen updating does not return true when you unload the userform and reload it.
    The initial true state is set by opening Excel not by the initialisation of VBA and does not reset until reverting back to Excel to start another procedure.Capture.JPG

  13. #13
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,539

    Re: Application ScreenUpdating and Error Handling

    Torachan, Your example is something else that I've been thinking about. In this example, the last False is because the main routine has not yet finished, eg
    Please Login or Register  to view this content.
    ScreenUpdating is automatically reset to True only after End Sub has been executed.
    I mean the cases where the macro has finished and the user sees old (stale) data in the spreadsheet, even though it has actually changed. Only, for example, manual editing of cells or switching to another sheet and returning will refresh the view. These are rare cases so I can't remember the details. But it seems to me (not sure) that this is what happens when the code is working with the PivotTable.

    Artik

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] VBA - Application.Screenupdating gives invalid use of property error
    By Simebaby in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-21-2021, 09:17 AM
  2. [SOLVED] Error handling for Combobox application vlookup...
    By Sintek in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-08-2018, 12:58 AM
  3. [SOLVED] Application.EnableEvents and Application.ScreenUpdating doubt
    By no_Fate in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-04-2017, 01:18 PM
  4. [SOLVED] VBA Application.EnableEvents and Application.ScreenUpdating
    By mcmunoz in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-01-2013, 10:24 PM
  5. [SOLVED] Dealing with Application Messages when running VBA - Error Handling
    By JungleJme in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-21-2012, 10:21 AM
  6. Application.ScreenUpdating not working with Application.Run
    By WaqasTariq in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-18-2012, 02:24 PM
  7. Application screenupdating error
    By ainnocent1983 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-28-2010, 10:29 AM

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