In Excel VBA, use ActiveWorkbook.Path to get the folder path of the workbook that is active; it does not include the workbook’s filename. Use ActiveWorkbook.FullName for the folder and filename together, and check whether the workbook has been saved before relying on either value.
What ActiveWorkbook.Path Returns
ActiveWorkbook refers to the workbook currently active in Excel’s window. Its Path property returns the location of that workbook as a string. For example, if the active file is Budget.xlsx in C:\Reports, then ActiveWorkbook.Path is C:\Reports, while ActiveWorkbook.FullName is C:\Reports\Budget.xlsx.
The related Name property returns only the filename, such as Budget.xlsx. These properties answer different questions, so choose based on what your macro needs:
ActiveWorkbook.Path: the containing folder.ActiveWorkbook.Name: the workbook’s filename.ActiveWorkbook.FullName: the full location and filename.
Do not add the filename to Path if you already use FullName; that would duplicate it. Likewise, a folder path alone is not a complete filename if you are passing it to code that expects a particular file.
A Safe VBA Example
This macro checks for an active workbook and for a saved location before displaying both the folder and the full filename. Put it in a standard VBA module, for example in the workbook’s VBA project, then run it while the workbook you want to inspect is active.
Sub ShowActiveWorkbookLocation()
Dim wb As Workbook
Dim folderPath As String
Dim fullFileName As String
' A workbook window may not be active.
If ActiveWorkbook Is Nothing Then
MsgBox "There is no active workbook."
Exit Sub
End If
Set wb = ActiveWorkbook
folderPath = wb.Path
' An unsaved workbook has no folder path yet.
If Len(folderPath) = 0 Then
MsgBox "Save the workbook before reading its path."
Exit Sub
End If
fullFileName = wb.FullName
MsgBox "Folder: " & folderPath & vbCrLf & _
"Full file: " & fullFileName
End Sub
Save a copy of the workbook before running a macro, especially if you adapt the code to move, rename, or save files. Macros cannot be undone with Ctrl+Z. On Windows, open the Visual Basic Editor with Alt+F11; on Mac, the usual shortcut is Option+F11, although some keyboards require the Fn key as well. Excel for the web does not run VBA macros; use desktop Excel.
ActiveWorkbook Versus ThisWorkbook
A frequent source of bugs is using ActiveWorkbook when the intended file is actually the one containing the macro. ThisWorkbook means the workbook whose VBA project is running. ActiveWorkbook means the workbook currently selected in Excel. They are often the same when a macro is stored in the file being edited, but they are not guaranteed to be.
For example, suppose a macro is stored in PERSONAL.XLSB, which loads in the background, and you run it while Sales.xlsx is active. In that case, ThisWorkbook.Path refers to the folder containing the personal macro workbook, whereas ActiveWorkbook.Path refers to the folder containing Sales.xlsx. The same distinction matters for macros stored in an add-in.
Choose deliberately:
- Use
ActiveWorkbook.Pathwhen the macro should work with the workbook the user is currently viewing. - Use
ThisWorkbook.Pathwhen it should work relative to the workbook that contains the macro. - Store a specific workbook in a variable when a macro opens or handles several files, rather than repeatedly relying on whichever workbook happens to be active.
Also remember that activating another workbook during a macro can change what ActiveWorkbook refers to. A variable such as Set wb = ActiveWorkbook captures the intended workbook object at that point, making later references easier to follow.
What Happens Before the Workbook Is Saved
A new workbook that has never been saved does not yet have a folder location. For that workbook, Path is an empty string, and FullName is not a dependable substitute for a saved file location. This is why code that builds an output filename from wb.Path should test that the path is not empty first.
To give the workbook a location, the user can save it with File > Save As in Excel for Microsoft 365 on Windows. In VBA, a macro can also use a save dialog or a method such as SaveAs, but choosing a destination or changing the workbook’s name has consequences. If the code saves a different file type, it may need to handle format compatibility and macro preservation. Do not silently save over an existing file unless that is an intentional part of the workflow.
A workbook that has been saved once and then edited normally retains its saved location even when it has unsaved changes. The path identifies the current saved file location; it does not mean every recent edit has been written to disk. If the macro needs the latest content preserved, saving the workbook is a separate decision from reading its path.
Building a File Path From the Folder
When you need a neighboring file, combine the folder with a filename and the platform’s path separator rather than assuming the separator character. Excel VBA exposes Application.PathSeparator for this purpose. For example, if the active workbook is saved and you want to construct a path for Notes.txt in the same folder, the general expression is:
Dim notesPath As String
notesPath = wb.Path & Application.PathSeparator & "Notes.txt"
This assumes wb has already been set to the workbook you intend to use and that its Path is nonempty. The example constructs a string; it does not create the text file or confirm that one exists. Use suitable file-handling code and error checks before reading or writing the result.
Avoid hard-coding a user-specific location such as C:\Users\Name\Desktop. Another person may have a different Windows account name, folder layout, or drive mapping. Building a path from the workbook’s actual folder is more portable when the related file is meant to travel alongside the workbook. If the file is meant to live in a fixed shared location, make that location configurable rather than assuming every user has the same setup.
Windows, Mac, and Cloud Locations
In desktop Excel for Microsoft 365 on Windows, a locally saved workbook commonly has a drive-letter path such as C:\Work\Book.xlsx. A workbook opened from OneDrive or SharePoint may be represented differently depending on how it was opened, Office’s settings, and whether a locally synced copy is involved. Do not assume every cloud workbook returns a traditional local folder path that another program can access.
Excel for Mac also supports VBA, but its file-system conventions differ from Windows. Avoid manually joining path pieces with a literal backslash when code should work across platforms. Application.PathSeparator can provide Excel’s path separator, but that alone does not make every Windows-specific file operation portable. Test file access on the platforms you support, and take care when a path points to a cloud service rather than an ordinary local folder.
Excel for the web is different: it can open and edit workbooks in a browser, but it does not execute VBA. A path-reading macro must be run in desktop Excel. For browser-based automation, Office Scripts or another supported workflow may be more suitable, though those tools do not use VBA’s ActiveWorkbook.Path property.
Common Errors and Misconceptions
- Expecting the filename from Path:
Pathreturns the folder. UseFullNamewhen you need the filename too. - Using ThisWorkbook for the selected file: In a personal macro workbook or add-in,
ThisWorkbookmay be the macro container, not the user’s file. - Ignoring an empty path: A workbook that has not been saved has no saved folder to return. Check
Len(wb.Path)before constructing a destination. - Assuming a local path for cloud files: A cloud location may not behave like a normal Windows folder. Validate what your workflow receives and whether the next operation supports it.
- Confusing the active workbook with the active sheet:
ActiveWorkbookidentifies a workbook;ActiveSheetidentifies a sheet or chart sheet. A sheet’s name is not a substitute for the workbook’s path.
When an unexpected value appears, inspect wb.Name, wb.Path, and wb.FullName together, and confirm which workbook was assigned to wb. This usually reveals whether the issue is an unsaved file, the wrong workbook reference, or an assumption about a cloud or network location.
Choosing the Right Property for a Task
For a message that tells a user where a workbook is stored, Path is usually readable because it omits the filename. For logging a particular file, FullName is usually more useful because it distinguishes files with identical names in different folders. For naming a backup based on the original file, start with Name or FullName and handle extensions and destination rules explicitly.
If a macro opens a workbook with VBA’s Workbooks.Open, assign the returned workbook to an object variable and use that variable’s properties. This is more reliable than assuming the opened file will remain active throughout all later steps. Similarly, if a macro creates a new workbook, remember that its path remains empty until it is saved somewhere.
There is no single path property that automatically answers every file-management question. Decide whether you need a folder, a filename, or a full location; identify which workbook object is the target; then account for whether it has been saved and where it is stored.
Frequently Asked Questions
How do I get the full path of the active workbook in VBA?
Use ActiveWorkbook.FullName. It returns the workbook’s location together with its filename. Check that an active workbook exists and that the file has been saved before relying on the value.
Why is ActiveWorkbook.Path blank?
The workbook may be new and not yet saved. Save it to a location first, then read Path. Also confirm that your code is referencing the workbook you mean, rather than another workbook or a workbook object that was never assigned.
Does ActiveWorkbook.Path include the Excel filename?
No. It returns the folder location only. Use ActiveWorkbook.FullName for folder plus filename, or combine Path with Name and the appropriate separator when you specifically need to build a path.
Should I use ThisWorkbook.Path or ActiveWorkbook.Path?
Use ThisWorkbook.Path for the workbook containing the running VBA code. Use ActiveWorkbook.Path for the workbook currently active in Excel. They can differ when code is stored in an add-in or PERSONAL.XLSB, or when another workbook is active.
Can VBA get the path in Excel for the web?
No VBA macro runs in Excel for the web. Open the workbook in desktop Excel to run VBA. A web-based automation task may call for Office Scripts instead, which is a separate programming model.
Related
- Excel Insert Page Breaks: Steps, Options, and Fixes
- Excel Box and Whisker Plot: How to Create One
- Excel Data Validation Lists From Tables, Explained
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Set Cell Value: Examples and Common IssuesTo set a cell value with Excel VBA, assign a value to a worksheet range, such as Range("A1").Value…
- Excel VBA New Line in a String: Practical ExamplesTo add a new line to a string in Excel VBA, join the text with a newline constant:…
- VBA Delete Row If Cell Contains TextTo delete rows when a cell contains particular text, use VBA’s InStr function in a loop that checks…
- Using VBA to Sort Contracts in Excel: A Practical LessonTo sort a contract register by expiration date with VBA, put the records in one rectangular range with…
- Microsoft Excel VBA For Loops: Syntax and ExamplesIn Microsoft Excel VBA, a For...Next loop repeats instructions a set number of times, while For Each...Next repeats…
- VBA Outlook: How to Create a Contact GroupIn classic Outlook for Windows, VBA can create a contact group by making an Outlook DistListItem, adding resolved…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.