Excel VBA: Open Files with Relative Paths in Workbooks

VBA & macros · Updated November 19, 2026

In Excel VBA, build a file’s path from the folder containing the macro workbook, then pass that full path to Workbooks.Open. For example, use ThisWorkbook.Path & Application.PathSeparator & "Input.xlsx" when the file sits beside the workbook that contains the code.

The basic relative-path pattern

A relative path identifies a file by its location in relation to a starting folder, rather than by spelling out a complete drive or network path. VBA’s ThisWorkbook.Path gives you the folder path of the workbook containing the running code. Add the file name to that folder and use the result as the Filename argument of Workbooks.Open.

For instance, if the macro workbook and Input.xlsx are both in C:\Reports\November, the expression constructs C:\Reports\November\Input.xlsx on Windows. The same workbook folder can be moved to another drive or computer without changing the code, provided the relative folder structure and file names stay the same.

Save a copy of the workbook before running a macro: VBA changes and file operations cannot be undone with Ctrl+Z. This simple example checks that the macro workbook has been saved, constructs the path, and opens the neighboring file read-only.

Sub OpenNeighborWorkbook()
    Dim basePath As String
    Dim fullPath As String
    Dim separator As String

    ' ThisWorkbook is the workbook that stores this macro.
    basePath = ThisWorkbook.Path
    If Len(basePath) = 0 Then
        MsgBox "Save this workbook before running the macro."
        Exit Sub
    End If

    separator = Application.PathSeparator
    fullPath = basePath & separator & "Input.xlsx"

    ' Open the source without allowing edits in this session.
    Workbooks.Open Filename:=fullPath, ReadOnly:=True
End Sub

Use the actual file name and extension, including spaces where present. If you want the opened workbook to be editable, remove ReadOnly:=True; doing so does not make the source safe from accidental changes.

Choose the right starting workbook

ThisWorkbook means the workbook in which the executing VBA code is stored. It is usually the right anchor for a macro-enabled controller workbook. By contrast, ActiveWorkbook means whichever workbook is currently active in Excel. That could change after another workbook opens, and relying on it can make a macro build a path from the wrong folder.

For example, if a macro stored in MonthlyTools.xlsm opens Input.xlsx, then ThisWorkbook.Path continues to refer to the folder containing MonthlyTools.xlsm. After the input file opens, ActiveWorkbook will often refer to Input.xlsx. If your code is stored in the Personal Macro Workbook, ThisWorkbook refers to that personal workbook, not to the workbook you happen to be working on. In that case, choose and validate the intended base workbook explicitly rather than assuming the active file is always the correct one.

Another useful distinction: ThisWorkbook.Name is the workbook’s file name, while ThisWorkbook.Path is its containing folder. Do not append a file name to a string that already includes the macro workbook’s own name. If you need the complete name and folder of the macro workbook, use ThisWorkbook.FullName.

Put the target in a subfolder

Relative locations can include folders below the macro workbook’s folder. Suppose the files are arranged like this:

  • Project\Automation.xlsm contains the macro.
  • Project\Data\Input.xlsx is the source file to open.

Build the path by adding the folder name and the file name as separate components. This example uses Application.PathSeparator rather than hard-coding a slash, which helps when adapting VBA to a different operating system.

Sub OpenFileInDataFolder()
    Dim basePath As String
    Dim fullPath As String
    Dim separator As String

    ' Save a copy of the workbook before running this macro.
    basePath = ThisWorkbook.Path
    If Len(basePath) = 0 Then
        MsgBox "Save this workbook before running the macro."
        Exit Sub
    End If

    separator = Application.PathSeparator
    fullPath = basePath & separator & "Data" & separator & "Input.xlsx"
    Workbooks.Open Filename:=fullPath, ReadOnly:=True
End Sub

For a file one folder above the workbook, a Windows-style relative path can use ..\Input.xlsx. However, constructing an absolute path from ThisWorkbook.Path and the known child folders is generally easier to read and debug. Avoid adding a separator twice: in the examples above, the base path is joined to each next component with exactly one separator.

Check that the file exists before opening

A missing file, misspelled folder, or changed extension can cause Workbooks.Open to raise an error or display an Excel prompt. Checking the constructed path first lets the macro give a more useful message. VBA’s Dir function can test whether a file with that path is present.

Save a copy before running this macro as well. It checks for a missing file and handles an error if Excel still cannot open the target—for example, because it is locked, inaccessible, or damaged.

Sub OpenInputWithChecks()
    Dim basePath As String
    Dim fullPath As String
    Dim separator As String

    ' This macro expects Input.xlsx beside the saved macro workbook.
    basePath = ThisWorkbook.Path
    If Len(basePath) = 0 Then
        MsgBox "Save this workbook before running the macro."
        Exit Sub
    End If

    separator = Application.PathSeparator
    fullPath = basePath & separator & "Input.xlsx"

    If Len(Dir$(fullPath)) = 0 Then
        MsgBox "File not found: " & fullPath
        Exit Sub
    End If

    On Error GoTo OpenFailed
    Workbooks.Open Filename:=fullPath, ReadOnly:=True
    Exit Sub

OpenFailed:
    MsgBox "Excel could not open the file: " & Err.Description
End Sub

The existence check is helpful, but it cannot guarantee that opening will succeed. A file may exist yet be unavailable because permissions have changed, another process has locked it, or Excel cannot read its contents. That is why the example also uses an error handler around the open operation.

Relative paths versus the current directory

A common misconception is that VBA automatically resolves a file name relative to the folder containing the macro workbook. It may instead resolve a relative file name against Excel’s current working directory, which can depend on how Excel was started or which code changed the current directory. Therefore, Workbooks.Open "Input.xlsx" is not a dependable way to mean “the file beside this workbook.”

Building a path from ThisWorkbook.Path removes that ambiguity. It also avoids changing the application’s current directory merely to make one file-opening command work. Changing the current directory can affect other file operations and make a macro’s behavior depend on its run history. For a workbook-specific task, an explicit path assembled from the workbook’s folder is clearer.

There is also a distinction between a relative path and a portable path. A path built from ThisWorkbook.Path is portable across locations only when the related files move together and retain the same layout. If the input file is on a fixed shared drive that is not inside the workbook’s folder tree, use the agreed network location or provide a way for users to select it.

Unsaved workbooks, cloud files, and shared folders

An unsaved workbook has no containing folder, so ThisWorkbook.Path is an empty string. The code should check for that condition before appending a separator and a file name. Save the macro workbook to its intended working folder first; otherwise, the macro cannot infer which folder should serve as the base.

For files stored in OneDrive or SharePoint, behavior depends on how the file is opened and whether it is synchronized to the computer. In Microsoft 365 desktop Excel for Windows, a synchronized folder is commonly available through a local Windows path, so nearby files can be addressed using the same folder-based pattern. A workbook opened through a cloud URL may expose a URL-like path or involve authentication and access rules that differ from ordinary local files. Do not assume a local relative-path example will work unchanged for every SharePoint or browser link.

VBA macros run in desktop Excel; Excel for the web does not run VBA macros. On Windows, network shares often use paths such as \\server\share\folder. A relative path based on the macro workbook works if both files are under the same accessible share and users have permission to open them. A mapped drive letter may not be available to every user, so a consistent shared location and permissions matter for a team workflow.

Windows and Mac considerations

The examples target Microsoft 365 Excel for Windows. Excel for Mac supports VBA, but file-system access, security prompts, and path handling can differ from Windows. Application.PathSeparator returns the separator appropriate to the Excel environment and is preferable to assuming that every system uses a Windows backslash. Keep the file and macro workbook together in a normal accessible folder, and test the workflow on the Mac configuration where it will be used.

Mac Excel may request permission when a macro accesses files in protected locations, such as some folders managed by macOS privacy controls. A valid-looking path does not override those permissions. If users see an access prompt or an error, check the file’s actual location and Excel’s granted access rather than repeatedly changing separators. Avoid hard-coding a Windows drive letter in a macro intended for Mac users.

For opening the workbook manually, Windows commonly uses Ctrl+O to open the file dialog; on Mac, use Command+O. Those shortcuts do not change how VBA resolves paths. A macro can also let a user browse for a file when its location is unpredictable, but that is a selection workflow rather than a relative-path workflow.

Common errors and practical safeguards

If the macro reports that a file is missing, inspect the final path it constructed. Check the spelling, extension, capitalization where relevant, folder names, and whether the workbook is saved in the folder you expect. Displaying fullPath in a message box or examining it in the VBA editor can reveal an extra separator or an unexpected base folder.

For a reliable routine, consider these safeguards:

  • Use ThisWorkbook.Path when the macro workbook is intended to define the base folder.
  • Check for an empty base path before building the target path.
  • Use Application.PathSeparator and keep folder components explicit.
  • Check that the file exists, then still handle errors from Workbooks.Open.
  • Choose deliberately whether to open the source read-only or allow edits.
  • Avoid broad error handling that hides failures; report the path or error so it can be diagnosed.

Also consider whether opening the file should update external links or display prompts. The default behavior may be appropriate for a simple task, but a production macro may need to set options such as UpdateLinks deliberately. These options affect how Excel opens a workbook; they do not repair an incorrect path or grant access to a restricted file.

Frequently Asked Questions

How do I open a file in the same folder as the VBA workbook?
Join ThisWorkbook.Path, Application.PathSeparator, and the file name, then pass the resulting string to Workbooks.Open Filename:=fullPath. Confirm that the macro workbook is saved and that the target file is actually beside it.

Why does Workbooks.Open not find a file using only its name?
A bare name does not reliably mean “beside the workbook containing this code.” Excel may look in its current working directory instead. Construct the full path from the intended base folder to remove that uncertainty.

What does ThisWorkbook.Path return if the workbook is not saved?
It returns an empty string when the workbook has not yet been saved to a location. Save the workbook first and check the path length in the macro before combining it with a separator and target file name.

Should I use ThisWorkbook.Path or ActiveWorkbook.Path?
Use ThisWorkbook.Path when the workbook that stores the macro is the intended base. Use another workbook’s path only when that is a deliberate part of the design; ActiveWorkbook can change as files open or users switch windows.

Can I use a relative path on Mac or with OneDrive?
Often, if Excel can access the files and the macro workbook and target retain the same folder relationship. Mac permissions and cloud URL or synchronization behavior can differ, so use Application.PathSeparator and verify the actual path and access in the target environment.

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.