+ Reply to Thread
Results 1 to 15 of 15

Run-time error '1004' when executing Exit Sub command line

  1. #1
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Run-time error '1004' when executing Exit Sub command line

    Run-time error '1004' when executing Exit Sub command line:

    The following code is run from a master workbook. It opens 4 other workbooks and exit. It does what it is supposed to do, but when it gets to the “Exit Sub” command line, it gives an error message of “Run-time error ‘1004’:
    Activate method of Range class failed.” 4 times.

    Could you please assist in solving this error?
    Best regards,
    Henk Stander

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Run-time error '1004' when executing Exit Sub command line

    You are only passing in the workbook name, i think the window reference is the full path. Not sure, i'll be in excel in a mo, so can check.

    Try thisworkbook.path

    ADDITION

    Should be ok, but thinking on, why not use thisworkbook.activate?
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Run-time error '1004' when executing Exit Sub command line

    What is strange to me is that the code runs and does everything it is supposed to do, but end up with the error message when it reaches the Exit Sub. What is also strange is that it gives the error 4 times. Maybe because it opened 4 files??

  4. #4
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Run-time error '1004' when executing Exit Sub command line

    Not sure with out seeing it in action, one of the workbooks may be slowing things down, i.e. still opening or something.

    Try taking them out and leaving 1 in each time, see if each one goes through ok.

  5. #5
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Run-time error '1004' when executing Exit Sub command line

    All workbooks open fully as per intention. I have tested the code to open only 1 file, and as I expected, I only got the error message once. I get the error message for every workbook opened, even though they did open as intended.

  6. #6
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Run-time error '1004' when executing Exit Sub command line

    i'd try workbooks(masterfile).activate or thisworkbook.activate

  7. #7
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Run-time error '1004' when executing Exit Sub command line

    I did, still getting the same result. Could it be caused by code in the workbooks that are opened? When I open them directly I do not get any errors.

    Code in the "ThisWorkbook" module of the workbooks being opened:
    Please Login or Register  to view this content.

  8. #8
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Run-time error '1004' when executing Exit Sub command line

    It could be, without seeing it i wouldnt know.

  9. #9
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Run-time error '1004' when executing Exit Sub command line

    Thank you for all your support so far nathansave!

    Ok. I have tested it. When I remove the code in the "ThisWorkbook" module of the workbook being opened, I do not get the error message.
    How then can I change the code of the macro in the masterfile to basically ignore the error?

  10. #10
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error '1004' when executing Exit Sub command line

    Why is the code in the ThisWorkbook module? It really belongs in a standard module (Insert>Module).

    Does this work?
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  11. #11
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Run-time error '1004' when executing Exit Sub command line

    Hi Norie,
    To clarify:
    The following code is in a standard module (i.e. the code in the masterfile):
    Please Login or Register  to view this content.
    The following code is in the ThisWorkbook module of each of the workbooks being opened by the code in the masterfile:
    Please Login or Register  to view this content.
    The code in the masterfile only needs to open the 4 workbooks referred to in the code and exit.
    It does that without a problem but after it has opened the 4 workbooks spew out the error message for each of the workbooks opened by the code.
    That is caused by the code in the ThisWorkbook module of the workbooks opened by the code.

    ThisWorkbook.Activate makes no difference to the result.

    Regards,
    Henk

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error '1004' when executing Exit Sub command line

    Try disabling events to the code in the workbook open events of the workbooks you are opening is not executed.
    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Run-time error '1004' when executing Exit Sub command line

    Hi Norie,
    You have done it!
    I added the line:
    Please Login or Register  to view this content.
    I accidentally added it as the very last command line in the sub (I meant to put it before the Exit Sub command line) and it worked. What I don't understand is that, because of the Exit Sub command line, this line should never be executed unless there was an error earlier in the code. Do you perhaps have an explanation?
    Please Login or Register  to view this content.
    Thanks again Norie.

  14. #14
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Run-time error '1004' when executing Exit Sub command line

    Actually that last line should be Application.EnableEvent = True to turn events back on, I forgot to add that.

    You should also add that just before Exit Sub.

  15. #15
    Forum Contributor
    Join Date
    05-02-2012
    Location
    Mosselbaai, Suid Afrika
    MS-Off Ver
    Excel 2016
    Posts
    107

    Re: Run-time error '1004' when executing Exit Sub command line

    Thank you very much Norie and Nathansav, it is working perfectly now.

    Regards,
    Henk

    Here is the final code:
    Please Login or Register  to view this content.

+ 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. Run Time Error 1004 when selecting range on another sheet using a command button
    By randolphoralph in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-06-2013, 07:43 AM
  2. Showing Run-time error "1004" when executing code with thousans of data
    By nigel_12 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-28-2013, 07:03 AM
  3. [SOLVED] Command Button to Call User Form - Run Time Error 1004
    By CRIMEDOG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-12-2013, 11:27 AM
  4. Run Time Error 1004 - You cannot use this command on a protected sheet
    By frogboy in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 12-11-2012, 07:59 AM
  5. Run time Error 1004 in 5th line of IF..THEN..ELSE
    By go-fish in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-19-2010, 05:17 AM

Tags for this Thread

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