+ Reply to Thread
Results 1 to 7 of 7

error handler when no workbook opened

  1. #1
    Registered User
    Join Date
    04-30-2019
    Location
    Bristol, England
    MS-Off Ver
    2016
    Posts
    8

    error handler when no workbook opened

    Hi All,

    Can someone help me out with error handler. Have a little code where it goes to different workbook does little editing copy it and paste it in original report based on todays date. I want to add error handler so when no workbook opened it would give a message that nothing opened. It works when nothing is opened but when I have report opened it still tries to error handle it. Probably it is something simple but I just cannot get my head around it. Thanks

    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: error handler when no workbook opened

    Put Exit Sub just before the Error handler to exit the code. Otherwise, it just runs the error handler every time it reaches the bottom of the code regardless if an error was thrown.

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 05-22-2019 at 11:51 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Registered User
    Join Date
    04-30-2019
    Location
    Bristol, England
    MS-Off Ver
    2016
    Posts
    8

    Re: error handler when no workbook opened

    Hi AlphaFrog, thanks for response.

    it works when report is opened but when it`s not it gives twice MsgBox and then goes to runtime error '1004' no data selected to parse

  4. #4
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Smile Re: error handler when no workbook opened

    you can use the debugger and follow the code in single steps. You will see when the ErrorHandler is executed.

    If you see the MsgBox twice then because you will get an error for both code lines between "On Error .."
    Please Login or Register  to view this content.
    with the second statement ".. Goto 0" you disable the error handler and now you get the standard error message "runtime error '1004' "

    Remove ".. Goto 0" and remove also "Resume Next" in the ErrorHandler section because you want to finish execution and exit the sub

  5. #5
    Registered User
    Join Date
    04-30-2019
    Location
    Bristol, England
    MS-Off Ver
    2016
    Posts
    8

    Re: error handler when no workbook opened

    Hi gue2013, thanks for response and advice using debugger. Kind of combined both advices and this is the code:
    Please Login or Register  to view this content.
    I places diferent handler before "Application.ActiveProtectedViewWindow.Edit" left "goto 0" to disable handlers, removed "Resume Next" and changed it to "Exit Sub"

    So it is working how I need. Thanks again for all help!

  6. #6
    Valued Forum Contributor
    Join Date
    01-14-2013
    Location
    Austria
    MS-Off Ver
    2016 / 2019
    Posts
    339

    Smile Re: error handler when no workbook opened

    I never used Application.ActiveProtectedViewWindow (I most probably need not use it) in the past and still not sure when or if to use it.

    The "Exit Sub" after "MsgBox" is not required because with "Exit Sub" you jump to "End Sub" which is the next statement after "MsgBox"

  7. #7
    Registered User
    Join Date
    04-30-2019
    Location
    Bristol, England
    MS-Off Ver
    2016
    Posts
    8

    Re: error handler when no workbook opened

    Thing is the people who is using the spreadsheet not really tech savvy and keep forgetting to enable editing and then complaining that nothing is working so that`s why I am using Application.ActiveProtectedViewWindow just simplify everything.

    And thanks for letting me know on "End Sub" I will declutter the code

+ 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] Error Handler with workbook open
    By TimlmiT in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-23-2018, 06:04 PM
  2. Why can't i copy from an opened workbook without an error??
    By dbravo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-24-2014, 12:28 PM
  3. Error handler for saving current workbook doesn't work
    By bemidjipatriot in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-25-2014, 03:43 PM
  4. Error Handler Help for Code that Copies a sheet to a new workbook
    By caliskier in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-27-2014, 12:47 PM
  5. Use a macro to extract cell data from opened workbook to new opened workbook
    By BrianTFC in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-26-2014, 01:35 PM
  6. [SOLVED] VBA Runtime error 9 when closing opened workbook
    By Deryl in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-24-2012, 09:18 AM
  7. UDF gives #VALUE! error when workbook opened, but F2/Enter clears it
    By 6StringJazzer in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-03-2011, 04:18 PM

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