To get the name of the workbook currently active in Excel VBA, use ActiveWorkbook.Name. For example, assign Set wb = Application.ActiveWorkbook and read wb.Name; use ThisWorkbook.Name instead when you specifically mean the workbook that contains the VBA code.
What “current workbook” means in VBA
Excel can have several workbooks open at once, so “current workbook” is not always a precise reference. VBA provides two commonly used workbook references, and choosing the right one is the key to returning the expected name:
ActiveWorkbookmeans the workbook in the active Excel window—the one the user is currently working in.ThisWorkbookmeans the workbook that contains the running VBA code.
Those references identify workbooks, not just names. Add the .Name property to either one to retrieve its filename. If a user opens a macro stored in a separate workbook, an add-in, or the Personal Macro Workbook, the active workbook may be different from the workbook containing the macro. That difference explains many cases where VBA returns a plausible but unexpected name.
Basic macro to show the active workbook name
In Microsoft Excel for Microsoft 365 on Windows, put this procedure in a standard VBA module to display the active workbook’s name in a message box:
Sub ShowActiveWorkbookName()
Dim wb As Workbook
' Refer to the workbook currently active in Excel.
Set wb = Application.ActiveWorkbook
If wb Is Nothing Then
MsgBox "No workbook is active."
Exit Sub
End If
MsgBox wb.Name, vbInformation, "Current workbook"
End Sub
Before running a macro, save a separate copy of the workbook. VBA macros cannot be undone with Ctrl+Z, even when a macro appears to make only a small change. This example only displays a message, but it is sensible to keep that habit for procedures you later adapt to edit cells or files.
To add the code, open the Visual Basic Editor with Alt+F11, choose Insert > Module, paste the procedure, and run it with the cursor inside the procedure or from Excel’s macro list. On Mac, Option+F11 opens the editor on many keyboards; depending on the keyboard settings, you may need Fn+Option+F11. Excel for the web can open a workbook but does not run VBA macros, so use the desktop application.
When to use ThisWorkbook.Name
If your code should report the name of the file where the macro is saved, use ThisWorkbook.Name. For instance, a macro stored in MonthlyReport.xlsm will refer to that workbook through ThisWorkbook, even if the user has clicked into a different open workbook before running the code.
This distinction matters especially for reusable automation. A macro in PERSONAL.XLSB is available across Excel sessions, but ThisWorkbook.Name then identifies PERSONAL.XLSB, not the user’s currently selected report. Likewise, code inside an add-in refers to the add-in through ThisWorkbook. If the task is to act on the workbook the user selected, ActiveWorkbook is generally the intended reference; if the task concerns the macro’s own file, use ThisWorkbook.
For a macro that should operate on a workbook chosen by the user, it is often safer to capture the reference once at the start and use that variable throughout. Otherwise, a change in the active window during a longer operation can make later references target a different workbook than earlier ones.
Name, FullName, and Path are different
The workbook name is not the same as its full file location. Excel exposes separate properties for these common needs:
wb.Namereturns the workbook’s filename, such asBudget.xlsx. For a saved workbook, this includes its file extension.wb.FullNamereturns the workbook’s full name including its path, such asC:\Reports\Budget.xlsx.wb.Pathreturns the folder path, such asC:\Reports, without the filename.
Use Name when displaying or comparing just the filename. Use FullName when a file operation needs the location as well, and use Path when you need the containing folder. Two different folders can contain files with the same name, so comparing only Name does not establish that two workbook references point to the same file.
For cloud or network locations, the returned path can look different from a simple local drive path, depending on how the workbook was opened and the storage setup. Avoid assuming every full path begins with a drive letter. If your code will build paths or open files, test it with the actual locations and use appropriate path handling rather than relying on a fixed path format.
How to choose the right reference
Choose based on what the macro is supposed to work on, rather than treating “current” as a synonym for one particular property:
- For the workbook currently selected by the user, use
ActiveWorkbook.Name. - For the workbook that stores the procedure, use
ThisWorkbook.Name. - For a workbook variable already set elsewhere, use that variable’s
.Nameproperty. - For a complete filename and folder, use
FullName, notName.
For example, a macro in a personal macro workbook that asks for the active report’s name should use Application.ActiveWorkbook.Name. A setup routine that labels the file containing its own code should use ThisWorkbook.Name. Being explicit about the intended workbook makes the code easier to review and reduces accidental edits to the wrong file.
If a macro opens another workbook, save a reference to the returned workbook object when you open it. Then use that object’s properties directly. This is more reliable than repeatedly asking which workbook happens to be active, because opening a file can change the active window and other actions can change it again.
Displaying, storing, or using the result
Once you have the workbook object, its name can be shown, printed for debugging, placed in a cell, or used as part of a file-handling routine. The suitable destination depends on the task. A message box is convenient for a quick check; the VBA Immediate window is useful while debugging; a worksheet cell is appropriate when the workbook name should become part of a report.
When writing the name to a worksheet, qualify the destination with the workbook and worksheet you mean. An unqualified Range or Cells reference can depend on the active sheet, which may not be the sheet you intended. Also decide whether a full filename such as Quarterly.xlsx is helpful in the cell or whether you need the location too. That is a choice between Name and FullName, not a formatting option applied to the same value.
For debugging, the Immediate window can show a value without changing a workbook. Open the Visual Basic Editor and choose View > Immediate Window, then run a small procedure that prints the property with Debug.Print. This is useful when checking whether a macro is targeting the active file or its own host file. Avoid putting a bare property expression in a procedure and expecting it to appear on screen; VBA needs an instruction such as MsgBox or Debug.Print to display or output the value.
Common problems and edge cases
If the macro returns a different workbook than expected, first check whether it uses ActiveWorkbook or ThisWorkbook. Then check which Excel window was active when the macro ran. A macro launched from the Personal Macro Workbook, an add-in, or the Visual Basic Editor can make this distinction less obvious than a macro launched from the report itself.
A new, unsaved workbook has no saved disk location yet. Excel gives it a temporary name, such as Book1, and Path is empty until the file is saved. Do not treat that temporary name as a permanent filename or use it to assume where the workbook exists on disk. After saving, the name and path reflect the saved file.
Other frequent sources of confusion include:
- Expecting a path from Name:
Namereturns the filename, not the containing folders. UseFullNamefor the complete location. - Comparing only filenames: two workbooks in separate folders can both be named
Report.xlsx. - Using a workbook reference when none is active: check that the object is set before using its properties, particularly in code that can run in unusual application states.
- Assuming a macro changes its own workbook: code can be stored in one file while working on another, so explicitly reference the intended workbook.
Running VBA safely in Excel
In desktop Excel for Windows, the Visual Basic Editor is available with Alt+F11. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon and select Developer. On Mac, use Excel > Settings (or Preferences, depending on the release) > Ribbon & Toolbar to show the Developer tab. Ribbon labels and the editor shortcut can vary slightly by platform, but the VBA object model concept of ActiveWorkbook and ThisWorkbook is the same.
Excel may block macros based on the workbook’s file type, security settings, or where the file came from. A workbook containing VBA should generally be saved in a macro-enabled format such as .xlsm; saving it as a macro-free .xlsx removes VBA code. Do not lower macro security globally just to run an unfamiliar file. Only enable or run macros when you trust the source and understand what the code does.
Keep a backup before running code that changes workbook content, names, or files. Excel’s undo history does not reverse VBA actions. If a macro will be used on important documents, test it first on a copy and confirm the selected workbook behavior before expanding it to perform more consequential operations.
Frequently Asked Questions
Does ActiveWorkbook.Name include the file extension?
For a saved workbook, yes. A saved file named Sales.xlsx is returned as Sales.xlsx, including its extension. An unsaved workbook can have a temporary name and has not yet acquired a saved file path.
What is the difference between ActiveWorkbook.Name and ThisWorkbook.Name?
ActiveWorkbook.Name is the name of the workbook active in Excel. ThisWorkbook.Name is the name of the workbook containing the running VBA code. They can refer to different files, especially when the code is in an add-in or PERSONAL.XLSB.
How do I get the full path of the current workbook?
Use ActiveWorkbook.FullName for the active workbook’s full name and location. Use ThisWorkbook.FullName if you need the location of the file containing the macro. An unsaved workbook does not yet have a normal saved location.
Can I get the workbook name with an Excel worksheet formula?
Excel’s CELL("filename",A1) can return file and sheet information for a saved workbook, but it returns more than just the filename and may require additional text extraction. VBA’s ActiveWorkbook.Name is the direct choice when the task is specifically to obtain a workbook name in a macro.
Can VBA run in Excel for the web?
No. Excel for the web does not run VBA macros. Open the workbook in the Excel desktop application to use the Visual Basic Editor and run VBA.
Related
- Excel Page Break Header: Repeat Headings on Every Page
- Excel Page Break Command: Insert, Move, and Remove
- Excel Page Break Character: Meaning and Print Breaks
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA: How to Close a UserForm After a Button ClickTo close an Excel VBA UserForm when a button is clicked, put Me.Hide in that button’s Click event…
- VBA Delete Whole Row: Safe Methods and ExamplesTo delete an entire worksheet row with VBA, use Rows(5).EntireRow.Delete, replacing 5 with the row number you want…
- VBA Find: Check Whether a String Contains a SubstringIn VBA, use InStr to check whether one string contains another: if the function returns a number greater…
- Excel Macro Filter: How to Automate Data FilteringTo filter a worksheet with a macro, use VBA’s AutoFilter method on a range that includes its header…
- Excel VBA: How to Find the Next Empty Cell in a ColumnTo find the next empty cell below the last entry in an Excel column, use VBA’s End(xlUp) navigation…
- Excel Macro Recorder: Keyboard Shortcuts and SetupYes. In Excel for Microsoft 365 on Windows, record a macro from Developer > Code > Record Macro…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.