Prevent Excel VBA From Activating the Wrong Workbook After a Macro Runs
Question details
The user needs an Excel VBA macro to keep the intended workbook active after creating a combo box and assigning code to it.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- A macro runs in one open workbook, creates a combo box, and attaches VBA code to the control.
- Observed behavior
- After the macro finishes, Excel activates a different open workbook even when the intended workbook is activated at the end of the code.
Open the macro in the Visual Basic Editor by pressing Alt+F11 and search the procedure for Application.Visible assignments. Also check any called procedures for the same setting, because application-level visibility affects every open workbook.
Avoid changing Application.Visible in the VBA macro
Use this solution when the macro hides and restores the Excel application window before the unexpected workbook switch occurs.
Changing Application.Visible to False and then True can cause Excel to restore focus to another open workbook. Hide the Visual Basic Editor window instead, so the Excel application visibility state is not changed.
In Excel, press Alt+F11 to open the Visual Basic Editor. In Project Explorer, open the module or workbook code that runs the combo box macro.
Press Ctrl+F in the Visual Basic Editor and search for Application.Visible. Remove or comment out statements such as Application.Visible = False and Application.Visible = True.
Where appropriate, use ThisWorkbook.VBProject.VBE.MainWindow.Visible = False to hide the Visual Basic Editor window without hiding the entire Excel application.
Return to Excel with Alt+Q, select the workbook that contains the macro, and run it again from Developer > Macros or the assigned button to confirm that the workbook remains active.

Qualify workbook and worksheet references when creating the combo box
Use this additional check if the macro still targets another workbook after Application.Visible has been removed.
Use WPS Spreadsheets for lightweight workbook and macro workflows
If Excel continues to change the active workbook during a VBA workflow, WPS Office provides a free, lightweight spreadsheet alternative with a familiar interface and smooth migration for common Excel files. Open and manage compatible workbooks in WPS Spreadsheets without changing your existing file format.

Frequently Asked Questions
Why does Excel activate another workbook after my VBA macro finishes?
A common cause is toggling Application.Visible. When Excel is hidden and shown again, it can restore focus to a different open workbook. Remove those statements and hide the Visual Basic Editor window with ThisWorkbook.VBProject.VBE.MainWindow.Visible = False instead.
Should I use ThisWorkbook or ActiveWorkbook in a macro that creates a combo box?
Use ThisWorkbook when the target is the workbook that contains the VBA project. Use a workbook variable assigned to a specific open file when the target is another workbook. Avoid ActiveWorkbook when focus may change during the macro.
How can I hide the VBA editor without hiding Excel?
In the VBA procedure, use ThisWorkbook.VBProject.VBE.MainWindow.Visible = False. This hides the Visual Basic Editor main window rather than changing the Excel application's visibility.
Why does activating the correct workbook at the end of the macro not always work?
Activation only changes the current window at that moment. Later events, visibility restoration, or code that uses ActiveWorkbook or ActiveSheet can still move focus. Qualify workbook and worksheet references throughout the procedure.




