How to Create Folders Using VBA (With Example)


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:\Users\Bob\Desktop\My_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:\Users\Bob\Desktop\My_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.

I could either create a folder with a different name or create the folder in a different location to avoid this error.

Additional Resources

The following tutorials explain how to perform other common tasks in VBA:

VBA: How to Count Number of Sheets in Workbook
VBA: How to Extract Data from Another Workbook
VBA: How to Delete Sheet if Name Contains Specific Text

Leave a Reply

Your email address will not be published. Required fields are marked *