VBA & Macro Problems

Prevent Excel VBA From Activating the Wrong Workbook After a Macro Runs

Kushani NimanthikaKushani Nimanthika Nov 12, 2026 869 views

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.

Prevent Excel VBA From Activating the Wrong Workbook After a Macro Runs
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the affected VBA procedure

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.

2
Find application visibility statements

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.

3
Hide the Visual Basic Editor window instead

Where appropriate, use ThisWorkbook.VBProject.VBE.MainWindow.Visible = False to hide the Visual Basic Editor window without hiding the entire Excel application.

4
Run the macro from the intended workbook

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.

Avoid changing Application.Visible in the VBA macro
VBA project access may be required: If Excel blocks access to VBProject, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and review the setting for trusting access to the VBA project object model.
Free Microsoft Office alternative

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.

Free and lightweight spreadsheet suite for everyday workbook tasksFamiliar ribbon-style interface for a smoother transition from Microsoft ExcelSupports opening and saving Microsoft Excel .xlsx and .xls filesKeeps spreadsheet editing, controls, and workbook management in one workspace
microsoft office alternative - wps office

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.