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
- Open the VBA editor. Press
Alt+F11in Excel. This opens the Visual Basic for Applications window. - 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.
- 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
- Run the macro. Place your cursor anywhere inside the
ShowFileNamesubroutine, then pressF5or click the green "Run" arrow. You will see two message boxes: the first shows just the file name, the second shows the full path. - 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
- 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
ThisWorkbookwithActiveWorkbook. For example:MsgBox ActiveWorkbook.Name. - Handle unsaved workbooks. If the workbook has never been saved,
Namereturns something like "Book1" (the default name) andFullNamereturns just the name without a path. To check if the workbook has been saved, useThisWorkbook.Path- if it returns an empty string (""), the workbook has not been saved yet. - Get the file name without extension (e.g., "Report" instead of "Report.xlsm"). Use the
FileSystemObjector 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
- Copy the file name to the clipboard (useful for documentation). Use the
DataObjectfrom 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
ActiveWorkbookwhen you meantThisWorkbook. If the user has another workbook open,ActiveWorkbookwill 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,
FullNamewill not show a path, andNamemight 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),
InStrRevreturns 0 andLeftgives an error. Add a check: ifInStrRev(fullName, ".") > 0then remove extension, else use the whole name. - Using
ThisWorkbook.Pathon 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
ThisWorkbookby 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
Nameproperty 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.Openmethod 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.