+ Reply to Thread
Results 1 to 25 of 25

Cannot close an open workbook from another workbook

  1. #1
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Cannot close an open workbook from another workbook

    I have workbookA on a network folder which when opened shows a splash screen form & does a bunch of stuff, and then opens workbookB (a larger file) on the users local folder.

    When workbookB opens I need it to close workbookA. I've tried so many ways to close it but keep getting 'subscript out of range'.

    Please help! I am using Excel 2010

    WORKBOOKA code:

    Please Login or Register  to view this content.

    WORKBOOKB code:

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    11-13-2013
    Location
    United States
    MS-Off Ver
    2010
    Posts
    147

    Re: Cannot close an open workbook from another workbook

    Subscript out of range can mean the file does not exist.

    Looking at the code you have .. I thought you said that the file that Workbook A opened was on the users local drive? That would normally be the C: drive.

    Your Workbook A code looks like it is opening a network mapped drive file and the Workbook B code looks like it is trying to close the local C: drive file.

    Just my observation .. may want to double check the drive letters and path.
    ----
    Mark threads as Solved
    Star those that help

  3. #3
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    Sorry, I just changed the drive letters for the purposes of this post.
    WORKBOOKA is on the network and WORKBOOKB is on the local folder, but I even tested WORKBOOKA on my C drive to close (as you can see in my code above). It is definitely not the spelling of the file path or file name before I posted.

  4. #4
    Forum Contributor
    Join Date
    11-13-2013
    Location
    United States
    MS-Off Ver
    2010
    Posts
    147

    Re: Cannot close an open workbook from another workbook

    Try changing this line

    Please Login or Register  to view this content.
    To this

    Please Login or Register  to view this content.
    Basically just remove the path information. If the workbook is open, it should close it by referencing the name of the workbook.

    Hope this helps.

  5. #5
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    That is great thanks, it works!
    Only problem now is that the StartForm I have showing in WORKBOOKB in workbookopen now closes after that line is executed Any ideas?

  6. #6
    Forum Contributor
    Join Date
    11-13-2013
    Location
    United States
    MS-Off Ver
    2010
    Posts
    147

    Re: Cannot close an open workbook from another workbook

    My Apologies .. I had been away on business and did not see the reply.

    Did you get this resolved?

  7. #7
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    Quote Originally Posted by ImStevenB View Post
    My Apologies .. I had been away on business and did not see the reply.

    Did you get this resolved?
    No I have not

  8. #8
    Forum Contributor
    Join Date
    11-13-2013
    Location
    United States
    MS-Off Ver
    2010
    Posts
    147

    Re: Cannot close an open workbook from another workbook

    Do you have any Sub's in Workbook A that execute on a Workbook Close?

  9. #9
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    Quote Originally Posted by ImStevenB View Post
    Do you have any Sub's in Workbook A that execute on a Workbook Close?
    No code in workbook close

  10. #10
    Forum Contributor
    Join Date
    11-13-2013
    Location
    United States
    MS-Off Ver
    2010
    Posts
    147

    Re: Cannot close an open workbook from another workbook

    Any chance you can either post or send me both workbooks without data in it? Then I can get a better look at it.

  11. #11
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    Sorry for the late reply...I figured out how to open another book and continue the code in the current book:

    In workbook A, after I hide the splash screen I wrote:

    Please Login or Register  to view this content.

  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: Cannot close an open workbook from another workbook

    Doesn't that end up with 2 instances of Excel open?
    If posting code please use code tags, see here.

  13. #13
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    It opens the workbook B in a new instance but then the next line 'thisworkbook.close false' closes workbook A and you are left with workbook B open, which is what I wanted.
    You have other suggestion perhaps?
    Thanks in advance

  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: Cannot close an open workbook from another workbook

    The instance of Excel that the first workbook was opened it will still be there, so you'll have 2 instances.

    That might not be a problem but it might not be a good idea either.

    Anyway, is there any code in the workbook you are opening that is run when it's opened?

  15. #15
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    In workbook B, I have a user form appear which interacts with the user and upon user entry it stores values in cells 'hidden' behind the form

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

    Re: Cannot close an open workbook from another workbook

    If that userform is being opened when Workbook B is opened then that's probably where the problem is.

    When the userform is opened code executon will move to it and it won't return to the code that opened the workbook until you hide/unload the userform.

    What do the two workbooks do?

    Could they be combined into one workbook?

  17. #17
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    You make a good point...at one point the user form hides to allow a user to paste into the excel book, however when the form hides you just see a blank excel file and no where do I find the instance of workbook B. However when I attempt to re-open workbook A, only then does workbook B magically appear. This is a problem! Is there a shell command I can execute?
    Last edited by mrfitness; 02-11-2014 at 12:23 PM.

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

    Re: Cannot close an open workbook from another workbook

    What's the purpose of each workbook?

  19. #19
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    Quote Originally Posted by Norie View Post
    What's the purpose of each workbook?
    Workbook A is a splash screen file on the network which checks for updates to ensure workbook b and the databases it works with are currently up to date
    It then opens wb B, which is the main application on their local CPU

  20. #20
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    Quote Originally Posted by Norie View Post
    If that userform is being opened when Workbook B is opened then that's probably where the problem is.

    When the userform is opened code executon will move to it and it won't return to the code that opened the workbook until you hide/unload the userform.

    What do the two workbooks do?

    Could they be combined into one workbook?
    It was originally in 1 large file on the network but I broke it out into 2 parts so it can open locally

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

    Re: Cannot close an open workbook from another workbook

    What happens if you change the code from the first post to this?

    WORKBOOK A Code
    Please Login or Register  to view this content.
    WORKBOOK B code
    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    You are right removing the path worked (was solved a few posts down on page 1)....the issue is that in workbook B I am showing a form on workbook open...and after executing the line to close workbook A from workbook B, the form in WB B hides automatically. If I try to show the form after closing WB A, it doesn't reach that line to execute.
    Last edited by mrfitness; 02-11-2014 at 06:07 PM.

  23. #23
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    After doing some more research I found the following solution in WORKBOOK B:

    WORKBOOK A Code
    Please Login or Register  to view this content.
    WORKBOOK B workbook_open code
    Please Login or Register  to view this content.

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

    Re: Cannot close an open workbook from another workbook

    So a timing issue perhaps, that kind of makes sense.

    If that works I would go with it rather than the creating a new instance of Excel.

  25. #25
    Registered User
    Join Date
    09-25-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2003 & Excel 2010
    Posts
    14

    Re: Cannot close an open workbook from another workbook

    Thank you to everyone for your help!

+ 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. Refresh all queries on workbook open, when finished print to pdf then close workbook
    By Bree Harvey in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-06-2014, 04:53 AM
  2. Transfer Data from Open workbook to Close workbook
    By alexnkc in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-13-2013, 10:46 PM
  3. Replies: 2
    Last Post: 09-11-2012, 09:42 AM
  4. How to open,save and close another workbook from the current workbook using macro?
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-15-2012, 10:42 AM
  5. Save an open workbook, then open template workbook and close the saved workbook
    By ondvirg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-27-2009, 10:20 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