Excel VBA ThisWorkbook: Step-by-Step Guide
In this article
ThisWorkbook is the VBA object that represents the workbook containing your macro code, and you reach it with a single line: ThisWorkbook. Unlike ActiveWorkbook, which follows whatever window the user clicks, ThisWorkbook always points to the file that holds the code, so it is the safe way to save, close, read sheets from, or log events for your own workbook. Nothing needs installing: the Visual Basic Editor ships with every desktop copy of Excel for Windows and Mac.
What You'll Need
- Excel for Windows or Mac (desktop). ThisWorkbook does not exist in Excel for the web.
- A macro-enabled workbook saved as .xlsm, or a personal macro workbook, so your code survives closing Excel.
- The Developer tab visible: right-click the ribbon, choose Customize the Ribbon, and tick Developer in the right-hand list (wording varies slightly by version).
- Macro security set to allow macros in trusted locations, or the file marked as trusted.
Step-by-Step
- Open the workbook you want the code to live in. ThisWorkbook refers to this exact file, so create or open the .xlsm file first. If you write code in a blank workbook and later copy it elsewhere, the object will point at the new host.
- Press Alt+F11 (Windows) or Fn+Option+F11 (Mac) to open the Visual Basic Editor. On the left you will see the Project Explorer; if it is hidden, use View > Project Explorer.
- Find your workbook in the Project Explorer. It appears as VBAProject (YourFileName.xlsm). Expand it and you will see a ThisWorkbook node and one node per sheet.
- Double-click ThisWorkbook. The code window opens. Anything you type here belongs to the workbook module, which is where workbook-level events such as Open, BeforeClose and BeforeSave live.
- Insert a standard module for ordinary routines. Use Insert > Module. ThisWorkbook is still usable from that module; it is a global object, not something limited to the workbook module. Keeping general code in modules and events in ThisWorkbook is the conventional split.
- Write a first test. In the module, type:
Sub ShowName()MsgBox ThisWorkbook.NameEnd Sub
Place the cursor inside the sub and press F5. A message box shows the file name of the workbook holding the code, even if another workbook is active. - Compare it with ActiveWorkbook. Add a second line using
ActiveWorkbook.Name. Click into a different workbook, run the macro again, and the two names will differ. That difference is the whole reason ThisWorkbook exists. - Use it to reach sheets and ranges. Write
ThisWorkbook.Worksheets("Sheet1").Range("A1").Value = "Hello". The sheet name must match exactly; if you rename the tab, update the string or use the sheet's code name instead, which you can see in the Properties window. - Save and close safely.
ThisWorkbook.Savesaves the file without prompting for a name.ThisWorkbook.Close SaveChanges:=Truecloses it. Avoid closing the workbook that is running the code while other macros still need it. - Add a workbook event if you need one. In the ThisWorkbook code window, choose Workbook from the left dropdown and Open from the right. Excel writes the procedure shell. Put your code between the lines; it runs each time the file opens, subject to macro security.
- Save the file as .xlsm. Use File > Save As and pick Excel Macro-Enabled Workbook. A plain .xlsx silently drops all VBA when saved.
- Test from a cold start. Close Excel completely, reopen the file, enable macros if prompted, and run the macro again. This catches code that only worked because the editor was already open.
Common Mistakes
- Using ActiveWorkbook where ThisWorkbook is meant. If the user switches windows mid-run, ActiveWorkbook changes and the macro writes to the wrong file. ThisWorkbook never drifts.
- Saving as .xlsx. The code disappears without a warning you will notice. Always check the extension.
- Assuming ThisWorkbook is the active file. It is the file that owns the code. A macro stored in Personal.xlsb will treat Personal.xlsb as ThisWorkbook, not the sheet you are looking at.
- Putting general routines in the ThisWorkbook module. They run, but they are harder to find and cannot be called from the Macros dialog as easily. Use standard modules.
- Hard-coding sheet names. Renaming a tab breaks
Worksheets("Sheet1"). Use the code name shown in the Properties window, or add error handling. - Forgetting that events need the exact procedure name and signature. Renaming
Workbook_Opento something else stops it firing.
Tips
- Use
ThisWorkbook.FullNamewhen you need the path, andThisWorkbook.Pathwhen you only need the folder. - Set
ThisWorkbook.Saved = Trueonly when you genuinely want to suppress the save prompt; it does not write anything to disk. - Store settings on a hidden sheet inside ThisWorkbook rather than in the registry, so the file stays portable.
- Add
Option Explicitat the top of every module. It forces variable declaration and catches typos that otherwise fail silently. - Keep a backup copy before editing workbook events, because a broken Open event can make the file hard to open normally. Hold Shift while opening to bypass events if that happens.
Troubleshooting
The macro does nothing. Check that macros are enabled for this file and that the code is in a module, not in a sheet or form module. Press F8 to step through line by line and see where it stops.
Compile error: Variable not defined. Either declare the variable or remove Option Explicit while testing, then declare it properly before shipping.
Wrong workbook is being changed. Search the code for ActiveWorkbook and ActiveSheet and replace them with ThisWorkbook and an explicit sheet reference.
Workbook_Open does not fire. Confirm the procedure sits in the ThisWorkbook module, is spelled exactly, and that the file was opened with macros enabled. If the file will not open at all, hold Shift during opening to skip events, then fix the code.
Code vanished after saving. The file was saved as .xlsx or .xls. Reopen the last .xlsm backup, or re-add the code and save with the macro-enabled format.
Permission or read-only errors on Save. The file may be open elsewhere, marked read-only, or sitting in a synced folder that is locked. Close other copies and check the file properties before retrying.
Frequently Asked Questions
What is the difference between ThisWorkbook and ActiveWorkbook?
ThisWorkbook is the workbook containing the running code. ActiveWorkbook is whichever workbook currently has focus. They are the same file only when that file is active.
Can I use ThisWorkbook in Excel for the web?
No. VBA, and therefore ThisWorkbook, is a desktop feature. The web version cannot run macros.
Does ThisWorkbook work in add-ins?
Yes, but it then refers to the add-in file, not the workbook the user is editing. Use ActiveWorkbook or pass the target workbook as a parameter when the code must act on the user's file.
How do I refer to a sheet by its code name?
Use the name shown in the Properties window, for example Sheet1.Range("A1"). Code names survive tab renames, which string names do not.
Why does ThisWorkbook.Name include the extension?
Name returns the file name as stored, including .xlsm. Strip it with string functions if you need a clean label.
Can I close ThisWorkbook from its own macro?
Yes, with ThisWorkbook.Close, but any code after that line will not run. Save first if changes matter.