In Excel VBA, declare a variable as a Workbook and assign the active workbook with Set wb = Application.ActiveWorkbook. For example, use Dim wb As Workbook first; the Set keyword is required because a workbook is an object.
What ActiveWorkbook Refers To
ActiveWorkbook is the workbook currently active in Excel—the workbook shown in the active Excel window. It is a property that returns a reference to a workbook object, so VBA can use that reference to read workbook properties or work with its worksheets. The word “active” describes Excel’s current interface state, not the workbook that contains your code.
That distinction matters when a macro is launched from a different workbook than the one it is meant to edit. For instance, a macro stored in Personal.xlsb can run while a separate report workbook is active. In that case, ActiveWorkbook refers to the report, while ThisWorkbook refers to Personal.xlsb. If the macro is stored in the report itself and that report is active, both references may happen to identify the same workbook—but that is not guaranteed.
Use ActiveWorkbook when your macro should operate on whichever workbook the user has selected. Use ThisWorkbook when the workbook containing the VBA code is the intended target. Choosing deliberately is more important than memorizing either name.
Basic Assignment Syntax
A workbook variable is commonly declared with the Workbook type, then assigned with Set. The following macro checks that Excel has an active workbook before storing its reference and displaying its name.
Before running a macro, save a copy of the workbook you intend to use. VBA changes cannot be reversed with Ctrl+Z, even if the macro looks simple.
Sub ShowActiveWorkbookName()
Dim wb As Workbook
' Stop if no workbook is active.
If Application.ActiveWorkbook Is Nothing Then
MsgBox "There is no active workbook."
Exit Sub
End If
' Store the active workbook object in the variable.
Set wb = Application.ActiveWorkbook
MsgBox wb.Name
End Sub
The variable wb now refers to the workbook that was active when the assignment ran. The macro can use wb.Name to get its name, wb.FullName to get its path and name, or wb.Worksheets to access its worksheets. The assignment does not make a new workbook or copy any data; it gives the variable a reference to an existing workbook.
Why VBA Requires Set
VBA distinguishes between assigning a value and assigning an object reference. A number, date, or text value can be assigned directly, but a workbook is an object. In VBA, Set tells the language to assign the object reference. Leaving it out from a statement such as Set wb = ActiveWorkbook causes an error rather than a valid workbook assignment.
The variable also needs a suitable declaration. Dim wb As Workbook makes it clear that wb is meant to hold a workbook. Although a Variant can hold an object when assigned with Set, using the specific type makes the code easier to read and helps the VBA editor identify workbook members such as Worksheets and Save.
For a normal macro, place the procedure in a standard VBA module. In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, then choose Insert > Module. On a Mac, VBA is also available in desktop Excel, but keyboard shortcuts and some editor details differ; use the Visual Basic command on the Developer tab if the Windows shortcut does not apply. Excel for the web cannot create, run, or edit VBA macros.
When to Capture the Active Workbook
Assign the workbook variable when you know which workbook the macro should target, usually near the beginning of the procedure. After Set wb = ActiveWorkbook runs, wb continues to refer to that workbook even if another workbook becomes active later. This is useful in a longer macro: subsequent steps can consistently act on the original target rather than whatever window happens to be in front at that moment.
For example, a macro might ask the user to select a workbook, then process it. Capture the reference immediately after the user’s selection, before opening another file or changing windows. If you write ActiveWorkbook repeatedly throughout a procedure, each use asks Excel for the workbook active at that moment. If focus changes during the macro, later operations could target a different workbook than earlier ones.
It is also worth checking for Nothing before using the reference. Excel may have no active workbook in situations such as having no workbook window open. Testing the object first prevents code from trying to read a property from a missing reference. The check should happen before assignment or before any later use, not after the macro has already attempted an operation on the object.
Work With the Workbook Through Its Variable
Once the reference is stored, qualify workbook-related actions with the variable. This makes it clear which workbook is being changed and reduces accidental dependence on the active sheet or active workbook. For example, the following procedure reports how many worksheets are in the workbook captured at the start.
Save a copy before running this macro, as with any VBA procedure. This example only displays information, but it demonstrates the same reference pattern used by macros that make changes.
Sub CountActiveWorkbookWorksheets()
Dim wb As Workbook
If Application.ActiveWorkbook Is Nothing Then
MsgBox "There is no active workbook."
Exit Sub
End If
Set wb = Application.ActiveWorkbook
MsgBox "Worksheets: " & wb.Worksheets.Count
End Sub
wb.Worksheets.Count counts worksheets in that workbook, not chart sheets. If your macro also needs chart sheets, use the workbook’s Sheets collection instead, which includes different sheet types. When referring to a particular worksheet, qualify it as wb.Worksheets("Data") rather than relying on ActiveSheet. That way, VBA looks for the sheet named Data inside the workbook held by wb.
A worksheet name must match the tab name exactly, including spaces. If a sheet might not exist, add error handling or check the workbook’s worksheets before using it. A variable reference makes the target workbook explicit, but it does not automatically ensure that every requested worksheet or range exists.
ActiveWorkbook Compared With ThisWorkbook
The most common mix-up is using ActiveWorkbook when the intended object is ThisWorkbook, or the reverse. ThisWorkbook means the workbook where the running VBA project is stored. It does not mean the workbook currently selected in Excel, and it does not change when the user activates another file.
- Use
ActiveWorkbookfor a macro intended to work on the workbook currently selected by the user. - Use
ThisWorkbookfor settings, templates, or data stored alongside the macro itself. - Use an explicitly identified workbook variable when the target is known by name or needs to remain the same throughout a procedure.
For instance, if a button in a macro workbook processes a report that the user has opened, the report is likely the active workbook and the button’s workbook is ThisWorkbook. If the macro should write its results into the file that stores the code, use ThisWorkbook instead. The right choice depends on where the data belongs, not on which expression is shorter.
Common Problems and How to Avoid Them
- Forgetting Set: Workbook variables hold object references, so use
Set wb = ActiveWorkbook. A plain assignment is not the correct syntax for a Workbook object. - Capturing the wrong window: The workbook that contains the macro may not be the active workbook. Decide whether the target is
ActiveWorkbookorThisWorkbookbefore assigning the variable. - Using an unqualified sheet reference: Expressions such as
Worksheets("Data")can refer to the active workbook. Preferwb.Worksheets("Data")when the intended workbook is already stored inwb. - No active workbook: Check whether
Application.ActiveWorkbookisNothingbefore using it. This matters especially in macros that can run without a normal workbook window open. - Assuming the file name is unique: A name without a path may not be enough to identify a particular open file if similar names are in use. If you need to select a workbook by name, confirm the exact open workbook and consider checking its
FullName. - Expecting a saved variable between runs: A procedure-level variable exists for the procedure’s execution. It does not automatically preserve a workbook reference after the macro ends. A later macro should capture or identify its target again.
When debugging, place the cursor on a statement and press F8 in the VBA editor to step through code on Windows. On Mac, the editor’s step command or its displayed shortcut may differ. Check the value of wb.Name before a change if you are unsure which file the variable identifies. This simple confirmation can catch a wrong-target problem before a macro edits data.
Choosing a Workbook by Name Instead
Sometimes the active workbook is not a reliable way to choose a target. A macro may need to work with a particular file that is already open, regardless of which workbook window is selected. In that case, search the open workbooks and assign the matching one. This example checks the workbook’s file name and leaves the variable as Nothing if there is no match.
Save a copy of any workbook that could be affected before running VBA. The example below only displays a message, but the same selection technique can be used in a larger procedure.
Sub FindOpenWorkbookByName()
Dim candidate As Workbook
Dim wb As Workbook
Set wb = Nothing
For Each candidate In Application.Workbooks
If candidate.Name = "Report.xlsx" Then
Set wb = candidate
Exit For
End If
Next candidate
If wb Is Nothing Then
MsgBox "Report.xlsx is not open."
Else
MsgBox "Selected: " & wb.FullName
End If
End Sub
This method only searches workbooks that are currently open in that Excel instance; it does not open a file from disk. The comparison uses the workbook’s name, including its extension. If multiple open workbooks can share a name because they are in different folders, compare candidate.FullName with the complete expected path instead. Another option is to ask the user to choose a file, then store the workbook reference after opening or identifying it.
Frequently Asked Questions
What is the exact VBA line to set the active workbook?
Declare the variable with Dim wb As Workbook, then use Set wb = Application.ActiveWorkbook. The shorter Set wb = ActiveWorkbook also works in Excel VBA; including Application makes the property’s owner explicit.
Do I need Set when assigning a workbook variable?
Yes. A Workbook is an object, and VBA uses Set to assign an object reference. Declare the variable as a Workbook and use Set in the assignment.
Does ActiveWorkbook mean the workbook containing the macro?
No. It means the workbook currently active in Excel. The workbook containing the running VBA code is ThisWorkbook. They are the same only when the code’s workbook is also the active one.
How can I keep the same workbook targeted if the user changes windows?
Capture the workbook once with Set wb = ActiveWorkbook, then use wb for later operations. The variable continues to refer to the captured workbook while the procedure runs, even if another workbook becomes active.
Can Excel for the web run this VBA code?
No. Excel for the web can open and edit many macro-enabled files, but it cannot run, create, or edit VBA macros. Use the desktop Excel application for Windows or Mac to work with VBA.
Practical Rule of Thumb
For the direct answer, use Dim wb As Workbook followed by Set wb = ActiveWorkbook. Before building on that line, decide whether “active” truly describes the workbook you want. Check for Nothing when a workbook window may not be open, qualify sheet references through wb, and use ThisWorkbook when the macro’s own file is the intended target. These habits make the reference predictable and help prevent a macro from acting on the wrong workbook.
Related
- Excel Slicer Font Size: What You Can and Can’t Change
- Excel Slicer: Group Dates by Month
- Excel Slicer Colors: How to Change and Customize Them
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Creating Macros in Google Sheets: A Practical GuideTo create a macro in Google Sheets, open the spreadsheet in a web browser and choose Extensions >…
- Excel VBA: Check If AutoFilter Is Active on a SheetIn Excel VBA, check Worksheet.AutoFilterMode to find out whether AutoFilter arrows are displayed, and check Worksheet.FilterMode to find…
- Excel VBA: Copy a Column and Paste Values Only SafelyTo copy a column in Excel VBA and paste only its values, copy the source column and use…
- VBA Range Object: How to Work with CellsThe Excel VBA Range object represents one or more cells on a worksheet, letting code read or change…
- VBA Copy and Paste a File: Practical Code and Safe StepsTo copy a file with VBA in Excel for Microsoft 365 on Windows, use the FileCopy statement or…
- Excel VBA Editor: How to Open and Use ItThe Excel VBA Editor, also called the Visual Basic Editor or VBE, is the desktop window where you…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.