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.
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:
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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Many times you need to select just the visible cells before taking some action. It is helpful to know how to make this ...
Discover MoreWrite out a check and you need to include the digits for the amount of the check and the value of the check written out ...
Discover MoreOne of the most basic of programming structures is the conditional structure: If ... End If. This tip explains how this ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments