To get the current file name in Excel VBA, use ThisWorkbook.Name for just the file name (e.g., "Report.xlsm") or ThisWorkbook.FullName for the full path plus name (e.g., "C:\Users\You\Documents\Report.xlsm"). If your code runs in the same workbook that contains the macro, ThisWorkbook is the safest choice. If you need the name of the workbook that is currently active (which might be a different file than the one holding the macro), use ActiveWorkbook.Name instead. This guide shows you exactly how to write these lines, where to put them, and how to handle common pitfalls like unsaved workbooks or files with special characters.

What You'll Need

  • Excel (any recent version, 2010 or later, including Microsoft 365).
  • A workbook where you can add a macro. If you are new to VBA, you need to enable the Developer tab (File > Options > Customize Ribbon > check "Developer").
  • Basic familiarity with opening the VBA editor (press Alt+F11) and inserting a module (right-click on any item in the Project Explorer > Insert > Module).

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 in Excel. This opens the Visual Basic for Applications window.
  2. Insert a module. In the Project Explorer (usually on the left), right-click on any workbook name (e.g., "VBAProject (Book1)") and choose Insert > Module. A blank code window opens.
  3. Write a simple subroutine. In the module, type the following (or copy-paste):
Sub ShowFileName()
    MsgBox "File name: " & ThisWorkbook.Name
    MsgBox "Full path: " & ThisWorkbook.FullName
End Sub
  1. Run the macro. Place your cursor anywhere inside the ShowFileName subroutine, then press F5 or click the green "Run" arrow. You will see two message boxes: the first shows just the file name, the second shows the full path.
  2. Use the name in your code. Instead of showing a message box, you can store the name in a variable or use it directly. For example:
Sub UseFileName()
    Dim myFileName As String
    myFileName = ThisWorkbook.Name
    ' Now you can use myFileName in your code, e.g., to save a copy
    ThisWorkbook.SaveCopyAs "C:\Backup\" & myFileName
End Sub
  1. If you need the active workbook's name (the one the user is currently looking at, which may be different from the macro's workbook), replace ThisWorkbook with ActiveWorkbook. For example: MsgBox ActiveWorkbook.Name.
  2. Handle unsaved workbooks. If the workbook has never been saved, Name returns something like "Book1" (the default name) and FullName returns just the name without a path. To check if the workbook has been saved, use ThisWorkbook.Path - if it returns an empty string (""), the workbook has not been saved yet.
  3. Get the file name without extension (e.g., "Report" instead of "Report.xlsm"). Use the FileSystemObject or simple string functions. The easiest way without extra references is:
Sub ShowNameNoExt()
    Dim fullName As String
    Dim baseName As String
    fullName = ThisWorkbook.Name
    ' Remove the extension (everything after the last dot)
    baseName = Left(fullName, InStrRev(fullName, ".") - 1)
    MsgBox baseName
End Sub
  1. Copy the file name to the clipboard (useful for documentation). Use the DataObject from the Forms library. First, go to Tools > References in the VBA editor, check "Microsoft Forms 2.0 Object Library", then use:
Sub CopyNameToClipboard()
    Dim clip As New DataObject
    clip.SetText ThisWorkbook.Name
    clip.PutInClipboard
    MsgBox "File name copied to clipboard."
End Sub

If the Forms library is not available, a simpler alternative is to display the name in a message box and manually copy it.

Common Mistakes

  • Using ActiveWorkbook when you meant ThisWorkbook. If the user has another workbook open, ActiveWorkbook will return that other file's name, not the one containing your macro. This is the most common source of errors.
  • Forgetting to save the workbook before testing. If you run the macro on an unsaved file, FullName will not show a path, and Name might show a temporary name like "Book1". Save the file first to see the real name.
  • Assuming the file name has a dot. If the file name has no extension (rare but possible), InStrRev returns 0 and Left gives an error. Add a check: if InStrRev(fullName, ".") > 0 then remove extension, else use the whole name.
  • Using ThisWorkbook.Path on an unsaved workbook. It returns an empty string, which might cause errors if you try to build a full path. Always check if the path is empty before using it.

Tips

  • Use ThisWorkbook by default in your macros unless you have a specific reason to refer to the active workbook. It makes your code predictable.
  • For file names with spaces or special characters, the Name property handles them automatically - you don't need to escape anything.
  • If you need the file name in a cell, assign it directly: Range("A1").Value = ThisWorkbook.Name.
  • When using the file name to save a backup, consider adding a timestamp to avoid overwriting: ThisWorkbook.SaveCopyAs "C:\Backup\" & Format(Now, "yyyymmdd_hhmm") & "_" & ThisWorkbook.Name.
  • To get the file name of a workbook that is not open, you would need to use the Workbooks.Open method first, but that's beyond the scope of this guide.

Troubleshooting

If the macro doesn't run: Make sure macros are enabled. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings, and choose "Enable all macros" (or "Disable VBA macros with notification" and then click "Enable Content" when prompted). Also, ensure your workbook is saved as a macro-enabled file (.xlsm) if you plan to reuse the macro.

If you get a "Compile error: User-defined type not defined" when using DataObject, you forgot to add the reference to Microsoft Forms 2.0 Object Library. Follow step 9 to add it.

If the message box shows a different file name than expected: Check if you are using ActiveWorkbook and another workbook is active. Switch to the intended workbook or change to ThisWorkbook.

If FullName returns an error or empty path: The workbook is likely unsaved. Save it first with Ctrl+S, then rerun the macro.

If the file name is not updating after you rename the file: VBA reads the name at runtime, so it should always be current. If you renamed the file externally while Excel is open, close and reopen the workbook to let Excel recognize the change.

Frequently Asked Questions

How do I get the file name of the workbook that contains the macro?

Use ThisWorkbook.Name. This always refers to the workbook where the VBA code is stored, regardless of which workbook is active.

What is the difference between Name and FullName?

Name returns only the file name with extension (e.g., "Sales.xlsx"). FullName returns the full path including drive and folders (e.g., "C:\Users\You\Documents\Sales.xlsx").

How can I get the file name without the extension?

Use the string manipulation method shown in step 8, or use CreateObject("Scripting.FileSystemObject") and call GetBaseName on the full path.

Can I get the file name of a closed workbook?

No, not directly with VBA. You would need to open the workbook first or use file system objects to list files in a folder and match names.

Why does ActiveWorkbook.Name sometimes return "Book1"?

Because that workbook has not been saved yet. "Book1" is the default temporary name. Save the file to see the real name.

Does the file name include the file extension?

Yes, Name and FullName include the extension, such as .xlsx, .xlsm, or .xlsb. If you need only the base name, strip it off as shown in step 8.