Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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

Written by Allen Wyatt (last updated August 24, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


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. Further, if you don't provide a full pathname in DirName (perhaps you provide only a simple name like "TempFiles" or "MyDirectory"), then the directory is created in the current directory. Finally, if you try to create a directory that already exists, VBA will generate an error.

There is one potential gotcha you need to be aware of: You cannot create multiple levels of directories in a single go. For instance, let's say you have an existing directory called "Budget" on the C: drive, and you try to create the following:

sTemp = "C:\Budget\2019\First Quarter"
MkDir sTemp

You may get an error if the 2019 directory doesn't already exist within the Budget directory. To avoid such an error, you need to build each directory level explicitly, as in this manner:

sTemp = "C:\Budget\2019"
MkDir sTemp
sTemp = sTemp & "\First Quarter"
MkDir sTemp

Why would you want to create a directory in your macro? One common reason is to create a place where you can store temporary files you are working with in the rest of the macro.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8851) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. 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

Simplifying the Font List

Excel normally displays the font list on the toolbar or using the very fonts it is displaying. Here's how to change that ...

Discover More

Default Click and Type Paragraph Style

When you use the Click and Type feature, Word uses applies the Normal style to the paragraph created. You can specify a ...

Discover More

Selecting a Graphic Behind a Text Box

How to select a graphic that is obscured by a text box can be perplexing. Here's an overview of the different ways you ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Calculating Only the Active Workbook

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

Discover More

Counting All Characters

Need to know how many characters there are in a workbook? You can find out easily with the handy macro introduced in this ...

Discover More

Stopping a Checked Box from being Unchecked

When creating user forms for use in Excel, you are provided with a range of controls you can add, including check boxes. ...

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}] (all 7 characters, in the sequence shown) 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 6 - 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.