Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Changing Directories in a Macro.
Written by Allen Wyatt (last updated April 4, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
VBA provides a very rich programming environment. You can do many things with macro code that you cannot necessarily do using the tools provided on Excel's ribbons. For instance, you may want to change the current directory in the middle of a macro. This may be necessary in order to find a particular file or to do some other file-oriented task. VBA provides the ChDir command to change directories. The syntax is as follows:
ChDir DirName
where DirName is the full pathname of the directory to which you want to change. If you do not use a string variable to specify the directory name, then DirName must be enclosed in quotes. If the directory name you supply does not exist, the command fails with an error.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8804) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Changing Directories in a Macro.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Do your macros seem to be disabled on your new machine? It could be because of the security settings in Excel. Here's ...
Discover MoreThe undo list can be a lifesaver when working in a macro. Unfortunately, the undo list is not preserved when you run a ...
Discover MoreIt can be frustrating when macros don't run as you expect. When it occurs, however, tracking down the cause can be even ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments