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

Wrapping Text around a Graphic in a Text Box

Word allows you to wrap text around a graphic or around a text box, but it won't allow you to wrap text in a text box around ...

Discover More

Complex Compound Formatting

Sometimes it can seem that the formatting needs of your document can easily outstrip the capabilities of Word. This is not ...

Discover More

Clearing and Deleting Cells

When you want to remove information from a worksheet, you can either clear cells or delete cells. This tip examines the ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Telling which Worksheets are Selected

If your macro processes information on a number of worksheets, chances are good that you need your macro to figure out which ...

Discover More

Expiration Date for Excel Programs

If you use Excel to create a macro-based application, you may want to make sure that your programs cease working after a ...

Discover More

Calculating Only the Active Workbook

When you enter information into a workbook, Excel automatically recalculates every worksheet in every open workbook on your ...

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. Maximum image size is 6Mpixels. 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 two more than 4?

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.