I'm working on a VBA project based in a workbook. The code opens a new workbook and inserts a bunch of data in multiple worksheets of this new workbook. I deactivated Screen Updating (Application.Screenupdating = False) so initially the screen stays focused on the original workbook and other workbook in the background. However, the screen switches to the new workbook once VBA code book activated. How can I prevent this from happening? Thanks!
Asked
Active
Viewed 81 times
-1
-
"new workbook once VBA code book activated." Dont your `workbook.activate` There is no need for such code when you explicitly refenrence your workbooks. Also see this post: https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba – Luuklag May 01 '18 at 07:16
-
Also can you please share your code with us, then we can identify what piece of it results in the undesired behaviour. Or even better would be if you provide us with an MCVE, please read up on that here: https://stackoverflow.com/help/mcve – Luuklag May 01 '18 at 07:19
1 Answers
-1
Do you still need the new workbook open?
If so, use the following code to activate the workbook that contains the code:
ThisWorkbook.Activate
If you don't need the new workbook open anymore then after your code is done use the following to close the new workbook:
ActiveWorkbook.Close True 'To close saving the workbook
ActiveWorkbook.Close False 'To close without saving

Ricardo A
- 1,752
- 1
- 10
- 16
-
Making use of the `ActiveWorkbook` is usually a a bad idea. You rather want to explicitly reference the workbook you wan't to close to avoid any surprises. Also read up on: https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba – Luuklag May 01 '18 at 07:18
-
True, but without any code i dont know if he is opening the workbook by assigning it to a variable or not. However he mentioned the second workbook is active when the script is finished, which is why i went to the activeworkbook. – Ricardo A May 01 '18 at 07:53