With two open (identical, but differently named) Workbooks I want to ensure that the vba-code executed is from the active Workbook

Viewed 40

I've got a problem with the execution of VBA-Code. We are using a Template-Model to create offers for our service. Once I have opened two Workbooks, e.g. to take or compare data from that other book or to work on more than one offer, and after processing my data I want to save the Workbook I have worked on with F12 (save as). Now it happens (by accident) that the wrong book is saved as and the VBA-Code is executed in the wrong Workbook. How can I make sure, that the correct Workbook is saved and the process After_Save is executed in the right workbook. Thanks for support, George

1 Answers

ThisWorkbook is the workbook the code is written in, while ActiveWorkbook is the workbook that has focus (is selected / is on top). Make sure you use ThisWorkbook to run the code in the workbook the code is written in.

Related