Excel VBA MkDir: Create Folders and Handle Errors Safely

VBA & macros · Updated November 3, 2026

Excel VBA MkDir: Create Folders and Handle Errors Safely

In Excel VBA, use the MkDir statement to create a folder: MkDir "C:\Reports\November". The parent folder, C:\Reports, must already exist; MkDir creates one directory level, not a missing chain of nested folders.

What MkDir does

MkDir is a built-in VBA statement for creating a directory in the file system. It is not an Excel worksheet function, and it does not put a folder inside the workbook. The folder is created at the path you supply, subject to the current user’s access permissions.

The basic syntax is:

MkDir pathname

pathname is a string expression containing the folder’s path and name. In a VBA procedure, that expression is usually held in a String variable. For example, a Windows path might be C:\Reports\November. The statement creates the final folder, November, only if the parent, Reports, already exists.

A common misconception is that MkDir creates every missing folder in a path. It does not. If C:\Reports is absent, asking VBA to create C:\Reports\November fails rather than creating both directories. Check the parent first, or create missing levels one at a time.

Create one folder with a VBA macro

In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, choose Insert > Module, and paste a procedure into the module. Before running any macro, save a copy of the workbook: VBA actions cannot be undone with Ctrl+Z.

This example checks that the parent folder exists, avoids trying to recreate an existing directory, and displays a message if a file-system error occurs. Change the paths to locations appropriate for your computer.

Sub CreateNovemberFolder()
    Dim parentPath As String
    Dim targetPath As String

    ' Set the existing parent and the new folder path.
    parentPath = "C:\Reports"
    targetPath = parentPath & "\November"

    ' Send unexpected file-system errors to the handler.
    On Error GoTo CreateFailed

    If Len(Dir(parentPath, vbDirectory)) = 0 Then
        MsgBox "The parent folder does not exist: " & parentPath
        Exit Sub
    End If

    If Len(Dir(targetPath, vbDirectory)) > 0 Then
        MsgBox "The folder already exists: " & targetPath
        Exit Sub
    End If

    MkDir targetPath
    MsgBox "Created: " & targetPath
    Exit Sub

CreateFailed:
    MsgBox "Folder creation failed: " & Err.Description
End Sub

The Dir function is used here to look for a matching directory. The vbDirectory argument tells VBA to include directories in the search. The existence check improves the message shown to the user, while the error handler catches problems such as denied access or a path that became unavailable. A check cannot guarantee that creation will succeed: permissions or the file system can change between checking and creating.

Create nested folders one level at a time

To create a hierarchy such as C:\Reports\2026\November, ensure that each parent exists before creating its child. A reliable pattern is to start from a known existing base directory and add levels in order. This example stops if the base folder is missing and creates each later level only when needed.

Sub CreateReportTree()
    Dim basePath As String
    Dim yearPath As String
    Dim monthPath As String

    ' This starting folder must already exist.
    basePath = "C:\Reports"
    yearPath = basePath & "\2026"
    monthPath = yearPath & "\November"

    On Error GoTo CreateFailed

    If Len(Dir(basePath, vbDirectory)) = 0 Then
        MsgBox "Create or select the base folder first: " & basePath
        Exit Sub
    End If

    If Len(Dir(yearPath, vbDirectory)) = 0 Then MkDir yearPath
    If Len(Dir(monthPath, vbDirectory)) = 0 Then MkDir monthPath

    MsgBox "Folder structure is ready: " & monthPath
    Exit Sub

CreateFailed:
    MsgBox "Could not create the folder structure: " & Err.Description
End Sub

Each MkDir call in this routine creates just one directory. The existence checks make the procedure usable again if the year or month folder has already been created. If the base folder is also meant to be created by the macro, add a check for its own parent and create the base level first; do not simply assume that a deep path will be built automatically.

Choose and build the path carefully

For Windows examples, backslashes separate folders. Hard-coded paths are easy to understand, but they can fail on another computer if a user name, drive letter, network connection, or folder layout differs. Before using a path in a production macro, decide whether it should be fixed, selected by the user, or derived from a known location.

  • Fixed folder: Suitable for a controlled workplace setup where every computer uses the same accessible location.
  • Workbook-relative location: ThisWorkbook.Path returns the folder containing the workbook. It is empty if the workbook has not yet been saved, so check that condition before building a destination from it.
  • User-selected location: A folder picker can let users choose where output should go, avoiding assumptions about their folder structure.
  • Network or synced folder: Confirm that the location is available and writable when the macro runs. A disconnected network drive or sync conflict can interrupt the operation.

Use a complete path when predictability matters. A relative path depends on VBA’s current working directory, which may not be the folder the user expects; that makes it harder to tell where the directory will be created. Also check that a generated folder name contains no characters Windows disallows, such as <, >, :, ", /, \, |, ?, or *.

Common errors and how to troubleshoot them

A failed MkDir call usually points to the path, the folder’s existing state, or access rights. A VBA error handler can show Err.Description, but the underlying cause still needs to be corrected. Check the exact path rather than suppressing every error and assuming the folder was made.

  • Parent folder is missing: Create or verify the parent before calling MkDir. VBA commonly reports a path-not-found error for this situation.
  • Folder already exists: Check for it before creating it, or decide whether the macro should reuse it. Calling MkDir on an existing directory can raise an error.
  • Permission denied: Choose a location the current Windows account can write to. A macro does not bypass operating-system permissions.
  • Invalid or malformed path: Inspect concatenation for missing separators, doubled or misplaced characters, and invalid folder-name characters.
  • Unavailable drive or share: Confirm that the drive is connected and that the user can reach the network location.

During development, avoid a blanket On Error Resume Next around the entire procedure. It can hide a failed creation and let later code behave as though the directory exists. If it is used narrowly for a specific check, restore normal error handling immediately and inspect the error state before continuing.

MkDir compared with other folder-creation options

For a small macro that creates a folder on a known path, MkDir is often the most direct option: it is built into VBA and needs no added reference. The trade-off is that it creates only one level at a time, so the code must handle parent folders, existing destinations, and errors.

Another option is the Windows Scripting.FileSystemObject, which offers methods such as CreateFolder and other file-system operations. It can be useful when a project already uses that object model, but it is not automatically a better choice for a single directory. Its behavior and availability should be considered for the target environment, and using it does not remove the need to handle permissions and existing paths. Avoid adding Windows API calls just to create an ordinary folder; they add complexity without solving the basic path checks.

Security and platform considerations

Folder creation is a file-system change, so macros should use destinations the user understands and expects. Do not silently create folders in sensitive or shared locations, and do not treat a path taken from a worksheet as trustworthy without checking it. A workbook copied from an unknown source can contain macros with broader behavior than folder creation, so follow your organization’s macro-security rules and inspect code before enabling it.

The examples above use Windows paths and are intended for desktop Excel for Microsoft 365 on Windows. Excel for the web cannot run VBA macros. Excel for Mac has VBA support in the desktop app, but Windows paths such as C:\Reports are not valid Mac folder locations; use a path format and destination appropriate to that Mac environment, then verify permissions there. The Visual Basic Editor shortcut on Mac may depend on the keyboard’s function-key settings; the Windows shortcut is Alt+F11.

Frequently Asked Questions

Does MkDir create parent folders automatically?

No. MkDir creates only the final directory in the path. Its immediate parent must already exist. For a nested structure, check and create each missing level in order.

How do I check whether a folder exists in VBA?

A common check is Len(Dir(folderPath, vbDirectory)) > 0, where folderPath contains the directory path. Use error handling as well, because an unavailable or problematic path can still cause trouble beyond a simple existence check.

Can MkDir create a folder if it already exists?

No. Calling it for an existing directory can raise a run-time error. Check first and decide whether to leave the existing folder in place, report it, or use it for the next step.

Why does MkDir return a path-not-found error?

The most common reason is that one or more parent directories do not exist. Also check for a misspelled path, an unavailable network location, or a drive letter that is not connected.

Can Excel for the web run a MkDir macro?

No. VBA macros run in desktop Excel, not Excel for the web. A workbook can be opened online, but use desktop Excel to run its VBA code.

Practical takeaway

Use MkDir when VBA needs to create one directory at a known, writable location. Check the parent and destination first, create nested paths one level at a time, and provide a useful error message rather than assuming the operation worked. These simple checks make a folder-creation macro easier to reuse and safer to run.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.