Learn how to use the Excel MKDIR statement with syntax and examples.
The Microsoft Excel MKDIR statement allows you to create a new folder or directory.
The syntax for the Microsoft Excel MKDIR statement is:
path is the folder or directory to create.
If path is a complex directory structure, the high-level directories must already exist or the MKDIR statement will raise an error.
For example, if you executed the following code:
The c:\Test directory must already exist. The MKDIR statement will only attempt to create the Excel directory under the c:\Test directory. It will not create the c:\Test directory itself.
The MKDIR statement can be used in the following versions of Microsoft Excel:
The MKDIR statement can be used in Microsoft Excel as the following type of function:
The MKDIR statement can only be used in VBA code in Microsoft Excel.
Let's look at some Excel MKDIR statement function examples and explore how to use the MKDIR statement in Excel VBA code:
In this example, the MKDIR statement would create a new directory called Examples under the c:\TOTN directory.
In this example, the directory called Files would be created under the c:\TOTN\Examples directory.
Question: I'm not sure if a particular directory exists already. If it doesn't exist, I'd like to create it using VBA code. How can I do this?
Answer: You can test to see if a directory exists using the VBA code below:
If Len(Dir("c:\TOTN\Excel\Examples", vbDirectory)) = 0 Then MkDir "c:\TOTN\Excel\Examples" End If
In this example, the code would first check to see if the c:\TOTN\Excel\Examples directory exists. If it doesn't exist, the MKDIR statement would create a new directory called Examples under the c:\TOTN\Excel directory.