Please Note: This article is written for users of the following Microsoft Excel versions: 2007 and 2010. 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: Creating a Directory in a Macro.

Creating a Directory in a Macro

by Allen Wyatt
(last updated May 12, 2016)

1

If you need to, you can create a disk-drive directory (folder) using VBA. This is done with the MkDir command, and is a remnant from the same command in earlier versions of BASIC. The syntax is:

MkDir DirName

where DirName is the full pathname of the directory you want to create. If you do not use a string variable to specify the directory name, then DirName must be enclosed in quotes. You might want to use this command if you want to create a directory where you can store temporary files you are building with your macro.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8851) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: Creating a Directory in a Macro.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Counting Document Lines

Need to know how many lines are in your document? Word provides a quick and easy way you can determine the information.

Discover More

Creating Compound Characters

Word provides access to a wide variety of characters either from the keyboard or from the Symbol dialog box. Up and above ...

Discover More

Adding a Macro to the Quick Access Toolbar

The Quick Access Toolbar is a great place to put anything for which you want quick access. (Duh!) Here's how you can add your ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (ribbon)

Removing All Macros

Macros are stored as part of a workbook so that they are always available when you have the workbook open. If you want to get ...

Discover More

Summing Only Visible Values

When you use SUM to determine the total of a range of values, Excel doesn't really pay attention to whether the values are ...

Discover More

Inserting Worksheet Values with a Macro

Macros are often used to process information in a worksheet. You may need your macro to change the values stored in cells; ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is three more than 8?

2013-09-03 08:42:37

Bryan

I thought this tip sounded familiar, and I could have sworn I wrote comments on it before. The Google cached version of this page (http://webcache.googleusercontent.com/search?q=cache:LfjdydhpuroJ:excelribbon.tips.net/T008851_Creating_a_Directory_in_a_Macro.html+&cd=1&hl=en&ct=clnk&gl=us) in fact shows three comments:

Barry Fitzpatrick 17 Jul 2013, 09:52

If you do not use the full path name then the folder will be created in whatever the "current directory" happens to be.

The current directory/folder can be found by the VBA command "CurDir" which returns the full pathname to the current directory.

The "current directory" can be changed using the "ChDir path" command.

Other Methods such as .SaveAs if the full path is not specified will save to the "currect directory" also.

Bryan 17 Jul 2013, 07:24

It didn't like my backslashes for some reason... you will have to use your imagination.

Bryan 17 Jul 2013, 07:22

Note that you can't create an entire directory tree in one go with MkDir. If you already have C:Folder1 and you want to create C:Folder1Folder2Folder3 you have to run MkDir twice:

MkDir "C:Folder1Folder2"
MkDir "C:Folder1Folder2Folder3"

Also, if you try to create a directory that already exists, you will get an error.


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.