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

Working while a Macro is Running

If you have a macro that takes a long time to process a workbook, you might want to continue working in Excel while the macro ...

Discover More

Flush Left and Flush Right On the Same Line

Want some text at the left margin and some at the right, all on the same line? It's easy to do if you use your tab stops ...

Discover More

Using Find and Replace to Pre-Pend Characters

Need to add some characters to the beginning of the contents in a range of cells? It's not as easy as you might hope, but ...

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)

Creating a String in a Macro

Need to put together a bunch of characters to create a text string? You can do it in your macros by using the String ...

Discover More

Documenting Changes in VBA Code

Your company may be regulated by requirements that it document any changes to the macros in an Excel worksheet. Your options ...

Discover More

Storing a User's Location before Running a Macro

Macros are often used to process information in a workbook. If your macro makes changes in what is selected in the workbook, ...

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 nine minus 5?

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.