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

Custom Formats for Scientific Notation

Excel allows you to format your numeric values in a wide variety of ways. One such formatting option is to display numbers in ...

Discover More

ExcelTips: The Macros (Special Offer)

ExcelTips: The Macros is the definitive reference on how to use macros in Excel. You can stay at the top of your Excel ...

Discover More

Saving and Closing All Open Documents

Want to close or save all your documents at the same time? This trick does it for you.

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)

Displaying the Selected Cell's Address

Need to know the address of the cell that is currently selected? The function and macro highlighted in this tip will come in ...

Discover More

Item Not Available in Library

When sharing workbooks with others, you may find that the macros in those workbooks may not work as you expect. This tip ...

Discover More

Declaring Variables

Macros depend on the use of variables to do their work. This tip examines how variables are declared in a macro, using the ...

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 8Mpixels. 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 6 - 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.