[Excel VBA] Getting the Current Folder (CurDir) and Its Caveats

When handling files in VBA, you may want to base operations on the current working directory, also known as the “Current Folder.” For example, if you save a file using only a filename without specifying a full path, it is saved in this current folder.

VBA provides the CurDir function to retrieve this folder path. However, this function behaves in ways that require caution, and in many cases, safer alternatives exist.

This article explains how to use the CurDir function and the critical points you must know for practical use.

目次

Complete VBA Code and Behavior

The following code displays the current folder path and then saves a new workbook without specifying a path (relying on the current folder).

Sub CheckCurrentDirectory()

    ' Declare variables
    Dim currentFolderPath As String
    Dim newBook As Workbook
    
    ' --- 1. Get and display the current folder ---
    currentFolderPath = CurDir
    MsgBox "The Current Folder is:" & vbCrLf & currentFolderPath
    
    ' --- 2. Save a workbook without specifying a path ---
    ' Add a new workbook
    Set newBook = Workbooks.Add
    
    ' Saving with just the filename saves it to the Current Folder
    newBook.SaveAs "NewFile_In_CurDir.xlsx"
    
    ' --- 3. Close the saved workbook ---
    newBook.Close
    
    MsgBox "Saved 'NewFile_In_CurDir.xlsx' in:" & vbCrLf & _
           currentFolderPath

End Sub

When you run this macro, a message box first displays the current folder. Then, a new Excel file named NewFile_In_CurDir.xlsx is created inside that folder.

[Critical] The Decisive Difference Between CurDir and ThisWorkbook.Path

The most important thing to remember when using CurDir is that the folder it points to does not necessarily match the folder where your macro workbook is located.

Property / FunctionLocation IndicatedReliabilityMain Usage
CurDirThe current working folder of the Excel application.Low (Changeable)Specialized cases only. Generally deprecated for standard file I/O.
ThisWorkbook.PathThe folder where the workbook containing the macro is saved.High (Constant)Strongly recommended as the base path for file operations.

The folder pointed to by CurDir can change easily through operations such as:

  • The user navigating to a different folder in the “Open File” or “Save As” dialogs.
  • Using the ChDir statement in VBA.

Because of this, depending on when the macro is executed, you might not know where CurDir is pointing. This often leads to files being saved in unintended locations or “File not found” errors.

Summary

  • CurDir is a function that retrieves Excel’s current working directory.
  • Executing SaveAs "Filename" without a path saves the file to the folder indicated by CurDir.
  • CurDir has low reliability because it changes easily based on user actions.

Conclusion: If you want to manipulate files relative to the folder where your macro workbook is located, do not use CurDir. Instead, always use ThisWorkbook.Path.

' The safe way to specify a path
myPath = ThisWorkbook.Path & "\NewFile.xlsx"

Adopting this practice ensures stable file operations that are not affected by the macro’s execution environment.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次