Return to VBA Code Examples

VBA ChDir Statement

ChDir Description

Used to change the current directory or folder.

Simple ChDir Examples

ChDir "C:\ProgramData"

This makes “C:\ProgramData’ the current folder.

The ChDir statement changes the default directory or folder but doesn’t  change the default drive.

So, to change the current folder to the other folder on the other drive, you should use the ChDrive together.

ChDir Syntax

In the VBA Editor, you can type  “ChDir(” to see the syntax for the ChDir Statement:

The ChDir statement contains an argument:

Path: A string expression representing a directory.

Examples of Excel VBA ChDir Function

Sub ChDir_Example1()
    ChDrive "C:\"
    ChDir "C:\Program Files (x86)"
    MsgBox CurDir
End Sub

This changes the current folder to “C:\Program Files (x86)”.

Sub ChDir_Example2()
    ChDrive "C:\"
    ChDir "C:\Windows"
    MsgBox CurDir
End Sub

This changes the current folder to “C:\Windows”.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! vba save as


Learn More!
vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples