+ Reply to Thread
Results 1 to 4 of 4

Workbook.Close closes the wrong Workbook?

  1. #1
    Registered User
    Join Date
    02-18-2019
    Location
    Austin, TX
    MS-Off Ver
    Office 365
    Posts
    26

    Workbook.Close closes the wrong Workbook?

    I've got some code that opens a workbook, does some work, and then closes it. The code is running from another workbook. This works unless both workbooks are already open when I run the Sub. Then it closes the wrong workbook - it closes the one that is running the sub.
    Anyone know why that would happen?

    Please Login or Register  to view this content.
    So, it it supposed to open 'export.xls', run a function, and then close it. And it does that UNLESS 'export.xls' is already open. In that case, it closes my oMain worksheet.
    Why does it do that?

    Thanks!

  2. #2
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: Workbook.Close closes the wrong Workbook?

    Try
    Please Login or Register  to view this content.

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Workbook.Close closes the wrong Workbook?

    I set up a test. The only difference was that my machine does not have an environment variable called APP_PATH. But otherwise I ran this code with both files open and it correctly closed the export one.

    The fact that you qualify the call to Close with oExporter should guarantee that the correct file is closed. I would put a checkpoint on that line and determine what oExporter.Name is before the Close is executed and see what comes up.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Registered User
    Join Date
    02-18-2019
    Location
    Austin, TX
    MS-Off Ver
    Office 365
    Posts
    26

    Re: Workbook.Close closes the wrong Workbook?

    Ha - yes, it is the same name!

    I'm working on an Excel that I did not create, and I know there is some kind of link between that 'export.xls' file and the main .xlsm file that is running the sub.
    So, after that call to Workbooks.Open, oExporter.Name is the name of my main .xlsm file, not 'export.xls' as I would have expected.

    Can two workbooks have the same name, even if the filenames are different?

    Hmm apparently this is a known issue with Excel:
    https://stackoverflow.com/questions/...e-wrong-object

    The 'accepted answer' is to quit Excel and relaunch, but that didn't work for me. The second answer does work.

    Please Login or Register  to view this content.
    Last edited by bsabiston; 02-22-2019 at 02:47 PM.

+ 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. Copy from first workbook, close open new workbook paste close
    By johncarr1952 in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 08-04-2021, 08:50 AM
  2. [SOLVED] VBA code to update query from a password protected excel workbook and then close workbook
    By MrChipper in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-14-2017, 07:41 AM
  3. [SOLVED] Code closes all but active workbook, also closes personal...
    By fourmurphys in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 08-24-2016, 03:43 PM
  4. [SOLVED] VBA to open new workbook and run macro from new workbook, which closes old workbook
    By Rerock in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2014, 10:45 AM
  5. [SOLVED] Code to cut cell from one workbook, close same workbook, and paste in different workbook
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 03-13-2014, 04:01 PM
  6. Macro runs on wrong workbook on close
    By Cumberland in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-30-2009, 07:47 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