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 / Function | Location Indicated | Reliability | Main Usage |
CurDir | The current working folder of the Excel application. | Low (Changeable) | Specialized cases only. Generally deprecated for standard file I/O. |
ThisWorkbook.Path | The 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
ChDirstatement 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
CurDiris a function that retrieves Excel’s current working directory.- Executing
SaveAs "Filename"without a path saves the file to the folder indicated byCurDir. CurDirhas 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.
