Excel VBA Get Workbook Name: Methods and Examples

VBA & macros · Updated November 21, 2026

In Excel VBA, use ThisWorkbook.Name to get the name of the workbook that contains the running code, or ActiveWorkbook.Name to get the name of the workbook currently active in Excel. Both return the workbook’s filename, including its extension; use FullName for the full path and filename.

The quickest way to get a workbook name

For a macro stored in the workbook whose name you need, the direct expression is ThisWorkbook.Name. For example, if the file is named QuarterlyReport.xlsm, this property returns QuarterlyReport.xlsm. The result is text, so you can print it, assign it to a variable, show it in a message, or use it to build a file name.

A small procedure makes the distinction visible in the VBA editor’s Immediate Window:

Sub PrintWorkbookNames()
    ' Print the workbook containing this VBA code.
    Debug.Print ThisWorkbook.Name

    ' Print the workbook currently active in Excel.
    If Not ActiveWorkbook Is Nothing Then
        Debug.Print ActiveWorkbook.Name
    End If
End Sub

Before running a macro, save a copy of the workbook. These examples only print information, but macros can make changes that Ctrl+Z cannot undo.

What ThisWorkbook and ActiveWorkbook mean

The key choice is not a difference in spelling; it is which workbook you intend to identify. ThisWorkbook is the workbook where the VBA project and running procedure are stored. ActiveWorkbook is the workbook that currently has focus in Excel. They are often the same when a user runs a macro from a workbook, but they can be different.

For instance, a macro stored in PERSONAL.XLSB can operate on a separate report that the user has selected. In that situation, ThisWorkbook.Name returns PERSONAL.XLSB, while ActiveWorkbook.Name returns the report’s filename. Similarly, code in an add-in refers to the add-in through ThisWorkbook, not automatically to the open document.

  • Use ThisWorkbook.Name when the macro needs to refer to its own file reliably.
  • Use ActiveWorkbook.Name when the macro is deliberately working with the workbook the user has selected.
  • Use a workbook variable such as wb.Name when earlier code has already selected the intended workbook explicitly.

Code that relies on the active workbook should account for the possibility that no workbook is active. The guard in the sample prevents an attempt to read a property from an empty ActiveWorkbook reference.

Name, FullName, and Path compared

Excel’s Workbook object exposes related properties that answer different questions. Name is just the file name, including the extension. FullName identifies the file by its full name and path when it has been saved to a location. Path gives the folder location without appending the workbook name.

  • wb.Name might return Sales.2026.xlsx.
  • wb.Path might return C:\Reports.
  • wb.FullName might return C:\Reports\Sales.2026.xlsx.

Use Name for a label or a comparison based only on the filename. Use FullName when you need to identify a particular saved file, since two different folders can contain workbooks with the same name. Use Path when you need the folder, for example to construct a related output location. Avoid manually joining a path and a name without considering separators; a backslash is customary in Windows paths, while Mac paths use different conventions.

Get the name in a variable or display it

Assign the property to a variable when you will use it more than once. Declaring the variable as a String makes its purpose clear. The following procedure prints the active workbook’s name, if a workbook is active:

Sub ShowActiveWorkbookName()
    Dim wb As Workbook
    Dim workbookName As String

    ' Stop if there is no active workbook.
    If ActiveWorkbook Is Nothing Then Exit Sub

    Set wb = ActiveWorkbook
    workbookName = wb.Name
    Debug.Print workbookName
End Sub

To inspect the output, open the Visual Basic Editor and choose View > Immediate Window. On Windows, Alt+F11 opens or returns to the Visual Basic Editor; on Mac, the equivalent is usually Option+F11, sometimes with the Fn key depending on keyboard settings. The menu route can be used if the shortcut is intercepted by the operating system.

If you want a message instead of printed output, use MsgBox workbookName after assigning the value. In a larger macro, printing to the Immediate Window is often less disruptive than showing a dialog every time the code runs.

Return the filename without its extension

Name includes the extension, such as .xlsx or .xlsm. To remove only the final extension, find the last period and take the text before it. This also handles names that contain earlier periods, such as Sales.Q1.Final.xlsx:

Sub PrintNameWithoutExtension()
    Dim wb As Workbook
    Dim fileName As String
    Dim dotPosition As Long

    Set wb = ThisWorkbook
    fileName = wb.Name
    dotPosition = InStrRev(fileName, ".")

    ' Keep the name unchanged if there is no extension-like suffix.
    If dotPosition > 1 Then
        fileName = Left$(fileName, dotPosition - 1)
    End If

    Debug.Print fileName
End Sub

For Sales.Q1.Final.xlsx, the printed result is Sales.Q1.Final. The check for a period after the first character avoids turning a name that begins with a period into an empty string. This example treats the last period as the extension separator, which is appropriate for ordinary Excel workbook filenames. If you are processing unusual names or files without extensions, decide how those cases should be handled rather than assuming every period marks an extension.

Choose the right workbook reference

Before writing a macro, identify whose name you need: the file containing the code, the file currently selected, or a particular workbook chosen by the procedure. Making that decision explicit prevents a common bug in automation: the macro reports its own workbook’s name when the user expected the open data file’s name.

For workflows with several open workbooks, capture the intended workbook in an object variable and use that same reference throughout the procedure. For example, after setting wb to a workbook selected by your code, wb.Name continues to refer to that object even if focus changes. By contrast, ActiveWorkbook can point to another workbook after a window activation or another action changes which file is active.

Also distinguish a filename from a workbook’s tab or window caption. Workbook.Name is the workbook file name, not a worksheet name. To get the name of the selected sheet, use a worksheet property such as ActiveSheet.Name; that is a different object and answers a different question.

Unsaved workbooks and common problems

A new workbook may have a temporary name such as Book1 before it is saved. Its Name property can still provide that current workbook name, but it is not yet a dependable disk filename. In particular, Path is an empty string for a workbook that has never been saved. If a macro needs a real folder or a full disk location, save the workbook first and check that Path is not empty.

Other frequent sources of confusion include:

  • Wrong workbook returned: the code uses ThisWorkbook when it should use ActiveWorkbook, or vice versa.
  • Extension still appears: that is expected for Name; remove the final extension separately if required.
  • Only a folder is returned: the code is reading Path, not Name or FullName.
  • Duplicate filename: two workbooks in different folders can have the same Name; compare FullName when the location matters.
  • Object error: the active workbook reference may not exist, so test for Nothing before reading its properties.

Workbook names may also change after a Save As operation. If code stores the old name in a variable and the file is then renamed, that variable remains the old text; read the property again when you need the current name.

Excel versions, Mac, and macro-enabled files

The basic VBA workbook properties described here are available in desktop Excel for Microsoft 365 on Windows and in desktop Excel for Mac. The workbook-selection and file-path details can differ by platform, but Name, FullName, and Path are the relevant object properties in both environments. Avoid assuming a Windows-style path separator when writing code intended to move between Windows and Mac.

Excel for the web can open and edit many workbooks, but it cannot run VBA macros or edit VBA code. Use desktop Excel to create and run these procedures. A workbook containing VBA should be saved in a macro-capable format such as .xlsm or .xlsb; the standard .xlsx format does not retain a VBA project. When saving a macro-enabled file, use a trusted workbook and follow your organization’s macro-security rules.

Frequently Asked Questions

How do I get the current workbook name in VBA?

Use ThisWorkbook.Name for the workbook containing the running macro. Use ActiveWorkbook.Name only when you specifically want the workbook that is currently active in Excel.

Does Workbook.Name include the file extension?

Yes. If the file is named Budget.xlsx, Workbook.Name returns Budget.xlsx. Remove the last extension separately if you need only Budget.

How do I get the full path and filename?

Read Workbook.FullName. For a workbook that has not been saved, save it first if your code requires a dependable disk path, and check Workbook.Path for an empty value.

What is the difference between ThisWorkbook and ActiveWorkbook?

ThisWorkbook is where the VBA code is stored. ActiveWorkbook is the workbook currently active in Excel. They can differ when code is stored in an add-in or PERSONAL.XLSB, or when the macro activates another file.

Can I use VBA to get a workbook name in Excel for the web?

No. Excel for the web does not run VBA. Open the workbook in desktop Excel for Microsoft 365 on Windows or Mac to use the Visual Basic Editor and execute the macro.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.