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

Setting Stable Column Widths in a PivotTable

When you update a PivotTable, Excel can take liberties with any formatting you previously applied to the PivotTable. Here's ...

Discover More

Flipping Landscape Orientation when Printing

When printing a worksheet, you may want to rotate the output on the page to fit a certain orientation. Excel doesn't allow ...

Discover More

Triggering an Event when a Worksheet is Deactivated

One way you can use macros in a workbook is to have them automatically triggered when certain events take place. Here's how ...

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)

Self-Aware Macros

Sometimes it may be helpful for a macro to know exactly where it is being executed. This tip provides a way that you can ...

Discover More

Selecting Columns in VBA when Cells are Merged

If you have a macro that selects different columns in a worksheet while processing information, you may get some undesired ...

Discover More

Counting Commas in a Selection

If you have a range of cells in which you want to count all the commas, there are several ways you can derive the figure you ...

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 8 - 3?

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.