VBA MkDir function

MkDir Function Description

The VBA MkDir functions creates a new directory as provided by the path variable.In case the directory exists an error will be raised.

The MkDir function is often used in tandem with the Dir function to confirm if the requested directory has already been created.


The syntax for the MkDir statement in VBA is:

MkDir( path )


The directory to create.

Other Notes

The MkDir function creates only the lowest directory in the provided file path. E.g.

MkDir "C:\Test\NewDirectory\"

Will create the directory NewDirectory only if the directory C:\Test exists.

To verify if a directory exists you can use the Dir function.

Example usage

The MkDir statement can only be used in VBA. Let’s look at some MkDir function examples and explore how to use the MkDir statement in VBA code:

Ceate new directory NewDirectory:

MkDir "C:\Test\NewDirectory"