How do I create a folder with VBA?

Creating a folder with VBA is a simple process. You can use the MkDir command to create a folder with VBA. The command requires one parameter, which is the path of the folder you want to create. You can also use the FileSystemObject object to create a folder with VBA. The CreateFolder method requires the path of the folder you want to create as the parameter. Once the folder is created, you can use the object to manipulate it as needed.


You can use the MkDir statement to create a folder using VBA.

Here is one common way to use this statement in practice:

Sub CreateFolder()
    MkDir "C:UsersBobDesktopMy_Data"
End Sub

This particular macro will create a folder called My_Data on the Desktop of my computer.

The following example shows how to use this function in practice.

Example: Create a Folder Using VBA

Suppose my Desktop currently has two folders in it:

Suppose I would like to use VBA to create a folder called My_Data on the Desktop.

I can create the following macro to do so:

Sub CreateFolder()
    MkDir "C:UsersBobDesktopMy_Data"
End Sub

Once I run this macro and open the File Explorer to navigate to the Desktop, I can see that this new folder has been created:

I can see that the new folder called My_Data has been created in the exact location that I specified.

Note that if this folder already existed in this location and I ran this macro, I would receive the following error:

The error box tells us that there is a Path/File access error because a folder with this name already exists in this location on my computer.

x