ThisWorkbook.Close fails when originally called from a different book

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
Hello,
I have some code, App A, in WBook A that opens WBook B, loads data and then closes WBook A...it works great. Now enter WBook C which can open WBook A and run App A. I end up with WBook B open, as I wish, but ThisWorkbook.Close fails. I have put this line in AppC right after the Call App A instruction but it leaves WBook C open. I have also tried putting Close C into App A but then it leaves WBook A open.

Book C calls Book A
Code:
Public Sub Open_A
...various 
Application.Run "BookA.xls!Load_BookB"
End Sub
Code:
Public Sub Load_BookB
'...various opening of B and loading data...works great
Application.ScreenUpdating = True ' Book B appears on Screen
Application.ScreenUpdating = False 
Call Close_Book_C
Application.ScreenUpdating = True
ThisWorkbook.Close SaveChanges:=False ' this fails
End Sub
This closes C but leaves A open


Alternatively, I have tried closing Book C from the code in App C
Code:
Public Sub Open_A
...various 
Application.Run "BookA.xls!Load_BookB" 
   'includes ThisWorkbook.Close, which does work to close A
ThisWorkbook.Close SaveChanges:=True 'this fails
End Sub
Any suggestions would be greatly appreciated!
David
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Problem solved...FYI

I have inserted a Sub into App A that tests if BookC is open. If it finds BookC open, then AppA does not attempt to close BookA it just exits the sub and control is returned to BookC. App C then closes Book A and then executes ThisWorkbook.Close on itself, BookC...and I am left with just just Book B open. If the new Sub finds that BookC is not open, it then executes ThisWorkbook.Close on Book A...also leaving just BookB open.
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,487
Members
452,917
Latest member
MrsMSalt

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