[Excel VBA] How to Change the Current Folder (ChDir, ChDrive)

VBA has a function called CurDir that gets the current working folder of the Excel application. Paired with this, there are commands available to change this current folder programmatically.

These commands are ChDir (Change Directory) and ChDrive (Change Drive). In this article, I will explain how to use them and provide important notes on handling the current folder.

目次

1. Changing the Folder Within the Same Drive (ChDir)

If you want to change the current folder to another folder within the currently active drive (e.g., the C drive), use the ChDir statement.

Code and Explanation

Sub ChangeCurrentDirectory_SameDrive()

    Dim newFolderPath As String
    ' Example: Change to the "Work" folder on the C drive
    newFolderPath = "C:\Work"

    ' Check if the folder exists
    If Dir(newFolderPath, vbDirectory) = "" Then
        MsgBox "The specified folder does not exist.", vbExclamation
        Exit Sub
    End If
    
    '--- Change the current folder ---
    ChDir newFolderPath
    
    '--- Verify the change ---
    MsgBox "Current folder changed to:" & vbCrLf & CurDir

End Sub
  • ChDir "Folder Path": Changes the current folder to the specified path.
  • Note: This command cannot move across drives. For example, if the current folder is on the C drive and you try to execute ChDir "D:\Data", the current folder will not change, and an error may occur.

2. Changing to a Folder on a Different Drive (ChDrive + ChDir)

If you want to change the current folder to a folder on a different drive (e.g., from the C drive to the D drive), a two-step process is required.

Code and Explanation

Sub ChangeCurrentDirectory_DifferentDrive()

    Dim newFolderPath As String
    ' Example: Change to the "Data" folder on the D drive
    newFolderPath = "D:\Data"
    
    ' Check if the folder exists
    If Dir(newFolderPath, vbDirectory) = "" Then
        MsgBox "The specified folder does not exist.", vbExclamation
        Exit Sub
    End If
    
    '--- Step 1: Change the current drive ---
    ChDrive newFolderPath
    
    '--- Step 2: Change the current folder ---
    ChDir newFolderPath
    
    '--- Verify the change ---
    MsgBox "Current folder changed to:" & vbCrLf & CurDir

End Sub
  • ChDrive newFolderPath: First, use the ChDrive statement to change the current drive. You can specify the full path as an argument, but only the drive letter part is actually used.
  • ChDir newFolderPath: After the current drive has switched to the target drive, use ChDir to change the current folder within that drive.

[Most Important] Why You Should Not Rely on the Current Folder

Even if you successfully change the current folder using ChDir, please remember that this is only a temporary state.

If the user browses a different folder using the “Open File” dialog while the macro is running, the current folder can easily change.

Therefore, when saving or loading files, I strongly recommend that you do not rely on CurDir. Instead, you should always construct complete file paths based on a reliable absolute path, such as ThisWorkbook.Path (the folder where the macro workbook is saved).

Use ChDir and ChDrive only in limited situations where changing the current folder itself is absolutely necessary, such as when integrating with specific legacy applications.

Summary

  • Moving within the same drive: ChDir "Path"
  • Moving to a different drive: Two steps: ChDrive "Path" and ChDir "Path"

Although the current folder can be changed, it is very volatile and unstable. You should avoid relying on it as a standard for file operations. Specifying exact paths using ThisWorkbook.Path is the foundation for creating robust VBA macros.

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

この記事を書いた人

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

目次