Switching to another workbook via VBA

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
Hello. I am using VBA to open another workbook and then close the workbook that holds the code. There are quite a few operations and I hide the activity by disabling ScreenUpdating. To make this work I have enabled ScreenUpdating before closing the original file...this means that the user sees some jumping around that I would rather avoid. If I do not do this it behaves unpredictably...sometimes it will execute a random module in the new book or just freeze on the original book. Any suggestions? Many thanks!

Code:
Workbooks("Input.xls").Activate 'this is the new book
 Application.EnableEvents = True
ActiveSheet.Protect Password:=PW
Application.ScreenUpdating = True
Workbooks("Choose Case.xls").Activate ' the original book with this code
ActiveWorkbook.Close SaveChanges:=False
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
To close the workbook that holds the code

Code:
[/FONT]
[FONT=Courier New]thisworkbook.close false 'true if you wish to[/FONT]
[FONT=Courier New]
 
Upvote 0
I tried it witout activating the original book and it worked nicely. Thank you!
 
Upvote 0
Code:
[/FONT]
[FONT=Courier New]Workbooks("Input.xls").Activate 'this is the new book
ActiveSheet.Protect Password:=PW
ThisWorkbooks.Close false[/FONT]
[FONT=Courier New]

If you run this code then
Input.xls will be activated after original book is closed.
You dont have to use
Workbooks("Input.xls").Activate
again because the book that contains code is already closed.:)
 
Upvote 0
oh, yes
Input.xls will be activated after original book is closed.
You dont have to use
Workbooks("Input.xls").Activate
again because the book that contains code is already closed.
Thanks. :)




 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,687
Members
452,938
Latest member
babeneker

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