Use the CurDir function in Excel VBA to get the current directory, and the ChDir statement to change it. CurDir returns the full path of the folder where Excel is currently looking for files (typically the folder of the active workbook), and ChDir updates this path so subsequent file operations use the new location. No special setup is required beyond having Excel installed and enabling macros if you plan to run VBA code.

What You'll Need

  • Microsoft Excel (any recent version: 2010, 2013, 2016, 2019, 2021, or Microsoft 365)
  • Macro-enabled workbook (.xlsm) or standard workbook (.xlsx) with macros enabled
  • Basic familiarity with the VBA editor (Alt+F11 opens it)

Step-by-Step

  1. Open Excel and create a new workbook or open an existing one.
  2. Press Alt+F11 to open the Visual Basic for Applications (VBA) editor.
  3. In the VBA editor, go to Insert > Module to add a new code module to your workbook.
  4. In the code window that appears, type or paste the following subroutine to get the current directory:
    Sub GetCurrentDirectory()
        Dim currentPath As String
        currentPath = CurDir
        MsgBox "Current directory is: " & currentPath, vbInformation, "Current Path"
    End Sub
    This code declares a string variable, assigns the result of CurDir to it, and displays it in a message box.
  5. To test this, press F5 while in the VBA editor, or return to Excel and run it via Alt+F8 > select GetCurrentDirectory > click Run.
  6. To set a new current directory, add another subroutine:
    Sub SetNewDirectory()
        Dim newPath As String
        newPath = "C:\Temp"  ' Change this to any valid folder path
        On Error GoTo ErrorHandler
        ChDir newPath
        MsgBox "Current directory changed to: " & newPath, vbInformation, "Success"
        Exit Sub
    ErrorHandler:
        MsgBox "Error changing directory: " & Err.Description, vbCritical, "Error"
    End Sub
    This code attempts to change the directory to C:\Temp (update the path as needed), handles errors (e.g., if the folder doesn’t exist), and notifies the user.
  7. Run the SetNewDirectory macro using Alt+F8 or within the editor. If successful, subsequent file operations in VBA (e.g., Workbooks.Open) will use this new path as the default location.

Common Mistakes

  • Using backslashes incorrectly: In VBA strings, a single backslash is an escape character. Always use double backslashes (e.g., "C:\\MyFolder") or forward slashes (e.g., "C:/MyFolder") in file paths to avoid syntax errors.
  • Assuming CurDir always matches the workbook’s folder: CurDir returns the current working directory, which can differ from the workbook’s location—especially if Excel was launched from another application or via command line. To reliably get the workbook’s folder, use ThisWorkbook.Path instead.
  • Not handling errors with ChDir: If the path is invalid, inaccessible, or on a disconnected network drive, ChDir throws a runtime error. Always wrap it in an error handler (as shown above) to prevent crashes.
  • Forgetting drive context: On Windows, ChDir can only change the directory within the current drive. To switch drives, use ChDrive first (e.g., ChDrive "D", then ChDir "D:\NewFolder").

Tips

  • Combine ChDrive and ChDir: To ensure a reliable path change across drives, use:
    ChDrive Left(newPath, 1)
    ChDir newPath
    This extracts the drive letter from the full path and sets it before changing the directory.
  • Use ThisWorkbook.Path for workbook-specific paths: If you want to reference files in the same folder as your macro-enabled workbook, use:
    Dim wbPath As String
    wbPath = ThisWorkbook.Path
    If wbPath = "" Then
        MsgBox "This workbook has not been saved yet.", vbExclamation
    Else
        MsgBox "Workbook is in: " & wbPath
    End If
    This avoids issues with CurDir drifting due to other processes.
  • Store paths in a variable for reuse: Instead of calling CurDir repeatedly, capture it once at the start of your macro and use the variable throughout:
    Dim baseDir As String
    baseDir = CurDir
    ' ... later ...
    Workbooks.Open baseDir & "\\data.xlsx"
    This keeps your code cleaner and more maintainable.
  • Normalize path separators: If your code accepts user input or reads paths from cells, replace forward slashes with backslashes:
    cleanPath = Replace(userInput, "/", "\\")
    This prevents inconsistencies with VBA file operations.

Troubleshooting

If you encounter issues when getting or setting the current directory, follow these steps:

  1. Check for typos in paths: Ensure paths in ChDir are spelled exactly correctly—including spaces and capitalization. Windows is case-insensitive, but VBA path handling can be strict about syntax.
  2. Verify the folder exists: Manually navigate to the target folder in File Explorer. If Windows cannot access it, neither can VBA. Also check for hidden or system attributes that might block access.
  3. Confirm permissions: You may lack write or execute permissions on a network or restricted folder. Try a local folder (e.g., C:\Temp) first to test.
  4. Reset the current directory: If CurDir returns an unexpected path, restart Excel. The current directory resets when Excel launches, so closing and reopening often resolves drift.
  5. Check macro security: If the Run option is grayed out or code doesn’t execute, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure Enable all macros (or at least Disable all macros with notification) is selected. Restart Excel after changing settings.
  6. Use the Immediate Window for debugging: In the VBA editor, press Ctrl+G to open the Immediate Window. Type ?CurDir and press Enter to instantly see the current path without writing a full subroutine.

Frequently Asked Questions

What does CurDir return if Excel is launched from the command line with a specific working directory?

CurDir returns the working directory set when Excel started via the command line. It may not be the folder containing the workbook unless explicitly launched from that location.

Can I use ChDir to switch to a network path like \\server\share?

Yes, but you must map the network location first or ensure it’s accessible. Use the UNC path directly in ChDir (e.g., ChDir "\\\\server\\\\share"), though some systems require a trailing backslash or mapped drive for reliability.

Why does ThisWorkbook.Path return an empty string?

This means the workbook has never been saved. ThisWorkbook.Path only works for saved files. For unsaved workbooks, use CurDir as a fallback, but be aware it reflects Excel’s current working directory, not the unsaved file’s intended location.

Is CurDir affected by the current user profile or Excel instance?

Yes. Each Excel instance maintains its own current directory, and CurDir is tied to that instance. If you open multiple Excel windows, each may have a different CurDir depending on how they were launched.

Does ChDir work in Excel Online or Excel for the web?

No. VBA macros—including ChDir and CurDir—are not supported in Excel Online. They only run in desktop versions of Excel (Windows or macOS). For web-based alternatives, use Power Automate or OneDrive/SharePoint integration via API.

How do I get the current directory without opening the VBA editor?

Create a simple macro as shown earlier, assign it to a button on the Quick Access Toolbar or a worksheet, and run it with one click. Alternatively, type =CELL("filename") in a cell: the result includes the full path (e.g., C:\folder[workbook.xlsx]Sheet1), though you’ll need text functions to extract just the folder.