Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA ThisWorkbook Activate: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To activate ThisWorkbook in Excel VBA, use ThisWorkbook.Activate in a standard module, or simply Activate inside the ThisWorkbook code module. This brings the workbook that contains the running macro to the front, even if another workbook is currently active. It is the reliable alternative to Workbooks("name.xlsm").Activate when you do not want to hard-code a file name.

What You'll Need

  • Excel with macros enabled (the .xlsm or .xlsb file format).
  • Basic familiarity with the VBA Editor: press Alt+F11 to open it.
  • Macro security set to allow macros, or the file stored in a Trusted Location. If macros are blocked, the code will not run at all.

Step-by-Step

  1. Open the VBA Editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or the Developer tab). The Project Explorer on the left lists open workbooks and their components.
  2. Decide where the code belongs. For a general-purpose macro, right-click your project, choose Insert > Module. For code that should run automatically with the workbook (for example on open), use the ThisWorkbook module instead.
  3. Write the activation line. In a standard module, type:
    ThisWorkbook.Activate
    Inside the ThisWorkbook module, you can shorten it to:
    Activate
    Both refer to the same workbook: the one containing the code.
  4. Wrap it in a procedure. A bare line outside a Sub will not run. For example:
    Sub BringMeForward()
        ThisWorkbook.Activate
    End Sub
  5. Run the macro. Place the cursor inside the Sub and press F5, or use the Macros dialog (Alt+F8) and choose the procedure. The workbook containing the code should come to the front.
  6. Test with another workbook active. Open a second workbook, switch to it, then run the macro from the first. This confirms the code activates the correct workbook rather than whichever one happens to be on top.
  7. Save the file as .xlsm. If you save as .xlsx, Excel strips the macros and the code disappears. Use File > Save As and choose Excel Macro-Enabled Workbook.

Menu labels and dialog wording can differ slightly by Excel version, platform and region, but the VBA Editor and the code itself are consistent across Windows and Mac.

Common Mistakes

  • Using ActiveWorkbook.Activate by mistake. That activates whatever workbook is already active, which does nothing useful. ThisWorkbook is the one that holds the code.
  • Putting the line outside a Sub. Code at module level does not execute; it must be inside a procedure.
  • Expecting it to work when the workbook is hidden. A hidden workbook cannot be activated. Unhide it first with ThisWorkbook.Windows(1).Visible = True.
  • Saving as .xlsx. Macros are removed silently, and the next time you open the file the code is gone.
  • Confusing ThisWorkbook with ActiveWorkbook in add-ins. In an add-in, ThisWorkbook refers to the add-in itself, which is usually hidden, not the file the user is working in.

Tips

  • Use ThisWorkbook.Activate when you want the macro's own file; use Workbooks("Report.xlsx").Activate when you need a specific other file.
  • If you also need a particular sheet visible, follow with ThisWorkbook.Worksheets("Sheet1").Activate.
  • To avoid screen flicker in longer macros, set Application.ScreenUpdating = False at the start and back to True at the end, but activate the workbook after restoring it.
  • For code that should run when the file opens, put Me.Activate or Activate inside the Workbook_Open event in the ThisWorkbook module.
  • Keep a short comment above the line explaining why the workbook is being brought forward; future you will thank you.

Troubleshooting

The macro runs but nothing changes. Check that the workbook is not hidden and that you are not already in it. Also confirm the code is in the right module and that you ran the correct procedure.

Run-time error 424 or 91. This usually means an object reference is wrong, for example a misspelled sheet name. Step through the code with F8 to see which line fails.

Macros are disabled. Look for the yellow security bar and enable content, or move the file to a Trusted Location via File > Options > Trust Center > Trust Center Settings > Trusted Locations. Only enable macros in files you trust.

The code vanished after saving. The file was saved as .xlsx. Re-add the code and save as .xlsm or .xlsb.

It works on Windows but not on Mac. Some window-related methods behave differently on Mac. Test on the target platform and avoid relying on window state where possible.

If that did not work, close and reopen Excel, then re-run. If the problem persists, export the module (right-click > Export File) as a backup, remove it, and re-import it to rule out corruption.

Frequently Asked Questions

What is the difference between ThisWorkbook and ActiveWorkbook?

ThisWorkbook always refers to the workbook containing the running code. ActiveWorkbook refers to whichever workbook is currently on top, which can change as the user clicks around.

Can I use ThisWorkbook.Activate in a standard module?

Yes. In a standard module you must write the full ThisWorkbook.Activate. Inside the ThisWorkbook module you can use just Activate.

Why does ThisWorkbook.Activate fail when the workbook is hidden?

A hidden workbook has no visible window to activate. Make it visible first, then activate it.

Does ThisWorkbook.Activate work in an add-in?

It refers to the add-in file, which is normally hidden, so it will usually fail or do nothing useful. Use ActiveWorkbook or a specific workbook reference instead.

Will ThisWorkbook.Activate switch to a specific sheet?

No. It only brings the workbook window forward. Add a separate line to activate the sheet you want.

Do I need to activate a workbook before writing to it?

No. Most operations work on a workbook reference without activating it. Activation is mainly for the user's view, and skipping it makes macros faster and less flickery.