thisWB.close closes ALL workbooks, not just the one specified!

thenapolitan

Board Regular
Joined
Sep 5, 2014
Messages
52
Hey All,

You guys have been a big help before. Hopefully we can get something going again.

So, I have been using the following code. Works PERFECTLY on my computer, but when I move it over to another computer it closes ALL the Workbooks and leaves me with a blank Excel application open.

Any ideas on why it is closing everything on a different computer, but works fine on mine? I'm pulling my hair out here! This shouldn't be this complicated. Both computers running Office Professional 2016. Mine is Windows 8.1, the other computer is Windows 7.

Code:
    Dim thisWB as Workbook, importWB as Workbook, miscWB as Workbook

    set thisWB = ThisWorkbook
    set importWB = Workbooks.Open(filepath & filename)

    'do some processing and importing and stuff

    For Each miscWB In Workbooks
        If Not (miscWB Is ThisWorkbook) Then miscWB.Close SaveChanges:=false
    Next miscWB

Cheers,

thenapolitan
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi thenapolitan,

Not sure why the code works on your machine and not others but see if changing this line of code...

Code:
If Not (miscWB Is ThisWorkbook) Then miscWB.Close SaveChanges:=False

...with this does the job:

Code:
If miscWB.Name <> ThisWorkbook.Name Then miscWB.Close SaveChanges:=False

HTH

Robert
 
Upvote 0
Hi Robert,

Thanks for the response! So, I changed my code to make it even more simple:

Went from this:
Code:
    [COLOR=#333333]Dim thisWB as Workbook, importWB as Workbook, miscWB as Workbook
[/COLOR]
    set thisWB = ThisWorkbook
    set importWB = Workbooks.Open(filepath & filename)

    'do some processing and importing and stuff

    For Each miscWB In Workbooks
        If Not (miscWB Is ThisWorkbook) Then miscWB.Close SaveChanges:=false
[COLOR=#333333]    Next miscWB
[/COLOR]

To this:
Code:
    [COLOR=#333333]Dim thisWB as Workbook, importWB as Workbook
[/COLOR]
    set thisWB = ThisWorkbook
    set importWB = Workbooks.Open(filepath & filename)

    'do some processing and importing and stuff
    
    importWB.Close

Seems WAY simpler, right? But it still attempts to close thisWB. I'm wondering if I have an "ActiveWorkbook" tag somewhere causing this to close. Will search on that as well, but I don't think I used that.
 
Upvote 0
Okay - I FOUND IT! I had an "ActiveWorkook" tag in my "importWB" that was causing it all to close! Crazy! It's all fixed (I think) and seems to place nice now.

Thanks for the help and getting me to think. :)
 
Upvote 0
Okay - I FOUND IT! I had an "ActiveWorkook" tag in my "importWB" that was causing it all to close! Crazy! It's all fixed (I think) and seems to place nice now.

So, nope, I haven't found the culprit here. It still attempts to close both "importWB" and "thisWB" only on the Windows 7 computers when I attempt to use
Code:
importWB.close

Ugh - I might pull what's left of my hair out because of this!
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,017
Members
448,937
Latest member
BeerMan23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top