VBA activates the wrong Workbook

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
On one workbook (A), I have a button that opens another workbook (Main.xls) and then proceeds to close the first workbook. The problem is that once it finishes closing Workbook A, if another workbook is open, it then maximizes whichever workbook it happens to feel like, and not necessarily the one I want it to, even though I have specifically activated it. Perhaps I could put a variable specifying the book I want in the BeforeClose Sub of Workbook A. (I do not always the same book to be active after closing Book A so it would have to be done with a variable.) Any suggestions would be greatly appreciated. Many thanks!

Code:
Public Sub Back_To_Main()
Application.ScreenUpdating = False
Application.EnableEvents = False
Call Open_Main 'OpenWorkbooks module
Workbooks("Main.xls").Activate
Application.ScreenUpdating = True
Application.EnableEvents = True
ThisWorkbook.Close False
End Sub
This is the code that opens the workbook that I want to be active
Code:
Public Sub Open_Main()
On Error GoTo ErrorHandler
Workbooks("Main.xls").Activate
ActiveSheet.Unprotect Password:=PW
Range("A14").Value = 0
Range("G17").Select
ActiveSheet.Protect Password:=PW
Exit Sub
ErrorHandler:
Workbooks.Open Filename:=ThisWorkbook.Path & "\Main.xls"
ActiveSheet.Unprotect Password:=PW
Range("A14").Value = 0
Range("G17").Select
ActiveSheet.Protect Password:=PW
End Sub

This is the ThisWorkbook Code that is used to prevent changes to the workbook (A) that is being closed.
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  Application.MoveAfterReturnDirection = xlDown
  ActiveWorkbook.Saved = True
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
 ActiveWorkbook.Saved = True
 Cancel = True
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Where are you actually running all this code?

It's pretty confusing, for me anyway.

You have code in workbook A that opend workbook 'Main.xls', then closes the 'first' workbook.

Which workbook is the 'first' workbook? Workbook A?

Why do you want/need any of the workbooks to be active?
 
Upvote 0
The 'first' workbook is Workbook A. There are two total A and Main. I am going from A to Main...
 
Upvote 0
PS There are a series of books in the project...I want only one to be visible and open at any one time
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

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