Open Workbook2 from Workbook1 and close workbook1

Oseitutuakrasi

New Member
Joined
Jun 7, 2023
Messages
15
Office Version
  1. 365
Platform
  1. Windows
I am trying to open a series of workbooks one at a time from command buttons. In each workbook I have a button on a form. For example, when I click the button on UserForm1 in WORKBOOK1, I open workbook2 and then close workbook1 in the process. Then in workbook2, I click a button and open workbook3, then close workbook2 etc. etc. Unfortunately, the previous workbooks don't close so I have all the workbooks open.
On the other hand, if I manage to close the previous workbook, then the UserForm in the current workbook is not displayed. For example if I open workbook2 from workbook1 and then manage to close workbook1, the userform in workbook2 is not shown, so I cannot click any button to open the next workbook. I have only one form in each workbook, and I use Workbook_Open() to show the form as the workbook is being open
(UserformX.Show). It appears that if I open Workbook2 from workbook1 and the close workbook1, workbook2's Open event is not executed. But if I DONT CLOSE workbook1, workbook2' Open event runs. I am using VBA for all this. If I am making sense, can someone help me please?
Good Day
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Here's my guess. Your userform resides in wb1 and if it remains open you can't close wb1. You should be able to open and close whatever wb(s) from your userform, but not wb1. It would be best to show your code that your using. HTH. Dave
 
Upvote 0
Here's my guess. Your userform resides in wb1 and if it remains open you can't close wb1. You should be able to open and close whatever wb(s) from your userform, but not wb1. It would be best to show your code that your using. HTH. Dave
 
Upvote 0
Thanks Dave. With your suggestion, I will tackle from another angle, such as closing userforms in wb1 as I open wb2 so I can close wb1. Great suggestion.
I thin this will work. Many thanks
Samuel
 
Upvote 0
Hello Dave,
as you suggested, I have copied my code below. I have also tried to explain the code in the commented lines.
=====================================================================

VBA Code:
Private Sub cmdNext_Click()

'COMMENT:  The next line of code is run from a button in Wb1 (cmdClick). It opens Wb2 (Exercise2.xlsm is a workbook)

Workbooks.Open ThisWorkbook.Path & "\Exercise2.xlsm"

' COMMENT: When Wb2 is loaded, I try to close the form that loaded it. Then I try to close Wb1 (that contained the form). No success.

Unload UserForm1
Workbooks("Exercise1.xlsm").Close SaveChanges:=False

'COMMENT: I have also copied the above two lines into the Wb2's Workbook_Open event hoping that when Wb2 is opened it will close Wb1. Yet no success

'Wb2 is loaded alright, but Wb1 is NOT closed; both are loaded with their userforms. 'It appears that when I open Wb2 from Wb1 with a code, it does not execute the Wb2's Workbook_Open event. (I tested this with a Msgbox). However, when I open Wb2 from the desktop, of course the Workbook_Open event runs.
End Sub
 
Upvote 0
I messed around with this for a while. It's some type of automation conundrum. You can't close the wb before opening another wb, and when you open another wb, the wb open event fires and shows the userform which hangs the closing of the original wb. You may need to re-work your approach and have 1 master wb that has all the code to open/close other wbs and draw whatever info you want from them. Sorry, no easy solution. Dave
 
Upvote 0
I messed around with this for a while. It's some type of automation conundrum. You can't close the wb before opening another wb, and when you open another wb, the wb open event fires and shows the userform which hangs the closing of the original wb. You may need to re-work your approach and have 1 master wb that has all the code to open/close other wbs and draw whatever info you want from them. Sorry, no easy solution. Dave
Thanks Dave. It appears simple but indeed no easy solution. I thought my Office has been corrupted so I reinstalled it, hoping....
Thanks for confirming for me. I will try your suggestion. Thanks again
Samuel
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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