Code stops running after ThisWorkbook.Close

AnnaHansen

Board Regular
Joined
Oct 27, 2014
Messages
58
I have a workbook (Workbook1) that outputs data into a separate template file (Workbook2).

code living in Workbook1 opens Workbook2, writes data to it, then makes Workbook2 the active window so the user can review and decide if they want to save it.

If they want to save it, there is a button on the sheet in Workbook 2 which calls a save routine in Workbook1

Code:
sub butt*******()
application.run "workbook1.xlsm!SaveLocation"
end sub

For the longest time I couldn't figure out why in workbook1 SaveLocation,
Code:
sub SaveLocation()
'asking the user where to save
workbook2.close false
thisworkbook.close false
end sub

would quit running before closing workbook1. Then I finally realized, that once workbook2 is closed, all code stops because that is where I started from with the button.

So, I changed workbook1 SaveLocation() and removed workbook2.close false.

At the end of workbook2 butt*******(), I added thisworkbook.close false

That didn't work. The code stops running after thisworkbook.close in workbook1 and workbook2 is still left open.

Not sure why the name of the button click sub is being asterisked out, but it's butt*******() any help much appreciated
 
Last edited:
If you really want to avoid the situation. Use actual workbook names and set them to object variables, stay away from the ActiveWorkbook and ThisWorkbook syntax when they can be confusing to the code or the user. If it is not straight forward, and you don't understand it, don't use it. Use an alternative syntax or method.

I do use object variables, not activeworkbook. I just called them 1 and 2 for simplicity in this conversation. The solution you last posted is what I started with and it didn't work. Not that thisworkbook.close didn't do what I wanted it to, but that it was never executed. After workbook1.close it stops running anything at all.

I am 100% self taught and a one-person operation. When one thing works and another doesn't, or in this case, nothing works, I try to understand why.
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I do use object variables, not activeworkbook. I just called them 1 and 2 for simplicity in this conversation. The solution you last posted is what I started with and it didn't work. Not that thisworkbook.close didn't do what I wanted it to, but that it was never executed. After workbook1.close it stops running anything at all.

I am 100% self taught and a one-person operation. When one thing works and another doesn't, or in this case, nothing works, I try to understand why.

I understand your curiosity, just offering some advice that will save you time and grief. You will eventually come to understand what we have discussed here.
Regards, JLG
 
Upvote 0
Hi, try this code for the button of workbook2
Rich (BB code):
Sub butt()
  Application.OnTime Now, "workbook1.xlsm!SaveLocation"
End Sub

Code in workbook1:
Rich (BB code):
Sub SaveLocation()
  Workbooks("workbook2.xlsm").Close False
  ThisWorkbook.Close False
End Sub
 
Last edited:
Upvote 0
... Then I finally realized, that once workbook2 is closed, all code stops because that is where I started from with the button.
That is correct! But the OnTime charges the code running from the application not from the workbook. Then it does not matter any workbook are open or not.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,442
Messages
6,124,886
Members
449,194
Latest member
ronnyf85

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