VBA macros · Workbooks and files

VBA macro to create a folder for each name in a list

Select a list of names in Excel and create one folder for each in a folder you choose. Existing folders are skipped and invalid characters replaced.

Windows + Mac

A new school year, a new batch of clients, a new project with twenty work packages: each needs its own folder. Creating them by hand in Explorer or Finder means typing every name again. With this macro you select the names in Excel, pick a parent folder and get one folder per name in a second.

VBA
Option Explicit

Sub CreateFoldersFromList()
    Dim lst As Range, c As Range
    Dim parent As String, nm As String, path As String
    Dim made As Long, existed As Long

    If TypeName(Selection) <> "Range" Then Exit Sub
    Set lst = Selection

    parent = PickFolder()
    If Len(parent) = 0 Then Exit Sub
    If Right$(parent, 1) <> Application.PathSeparator Then parent = parent & Application.PathSeparator

    On Error GoTo Fail
    For Each c In lst.Cells
        nm = CleanFolderName(c.Text)
        If Len(nm) > 0 Then
            path = parent & nm
            If Len(Dir(path, vbDirectory)) > 0 Then
                existed = existed + 1
            Else
                MkDir path
                made = made + 1
            End If
        End If
    Next c

    MsgBox made & " folder(s) created, " & existed & " already existed.", vbInformation
    Exit Sub

Fail:
    MsgBox "Could not create '" & nm & "': " & Err.Description, vbExclamation
End Sub

Private Function CleanFolderName(ByVal s As String) As String
    Dim ch As Variant
    For Each ch In Array("\", "/", ":", "*", "?", """", "<", ">", "|")
        s = Replace(s, ch, "-")
    Next ch
    s = Trim$(s)
    Do While Right$(s, 1) = "."           ' Windows does not allow a trailing dot
        s = Left$(s, Len(s) - 1)
    Loop
    CleanFolderName = s
End Function

Private Function PickFolder() As String
    ' Windows: folder dialog. Mac: type or paste the path.
    #If Mac Then
        PickFolder = InputBox("Full path of the folder:", "Pick a folder", ThisWorkbook.Path)
    #Else
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Pick a folder"
            If .Show = -1 Then PickFolder = .SelectedItems(1)
        End With
    #End If
End Function
Create one folder per selected name

How to use it

  1. Open the workbook and press Alt+F11 (Mac: Opt+F11 or Fn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it.
  2. Choose Insert › Module. An empty code window opens on the right.
  3. Paste the whole code into that window. If the module already starts with Option Explicit, do not paste that line twice.
  4. Go back to Excel, press Alt+F8 (Mac: Opt+F8, or Tools › Macro › Macros), pick CreateFoldersFromList and click Run.
  5. Select the cells with the folder names, then run the macro and choose the parent folder.
  6. Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.

To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.

How it works

The selection is stored in lst before the folder dialog opens. The dialog returns the parent folder, and a path separator is added so each name can be appended directly.

CleanFolderName makes each name safe: the characters \ / : * ? " < > | are not allowed in Windows folder names, so they become dashes, spaces at either end are trimmed and trailing dots removed. A name such as Smith/Jones becomes Smith-Jones instead of accidentally creating a subfolder.

Dir(path, vbDirectory) returns something when the folder already exists, so the macro counts it and moves on. Otherwise MkDir creates the folder. MkDir can create only one level at a time: the parent must already exist, which it does because you picked it. If anything fails, for example a missing permission, the handler names the folder that caused it. File and folder commands are explained in Files and folders: open, save, loop through files and write text files.

Change it

To create a subfolder structure inside every new folder, for example Invoices and Contracts, add two lines after MkDir path:

VBA
                MkDir path & Application.PathSeparator & "Invoices"
                MkDir path & Application.PathSeparator & "Contracts"
Add standard subfolders to every new folder

To put a number in front of each name so the folders sort in list order, build the name as Format(made + existed + 1, "00") & " " & CleanFolderName(c.Text). To create the folders inside the workbook's own folder without a dialog, set parent = ActiveWorkbook.Path & Application.PathSeparator. To add a hyperlink to each new folder next to its name, use ActiveSheet.Hyperlinks.Add c.Offset(0, 1), path, , , "Open folder".

What can go wrong

If a name in the list contains a path separator on purpose, such as 2026\Q1, it is turned into a dash; the macro deliberately creates one level only. Very long paths (over 260 characters in total on older Windows setups) cause Path not found. Empty cells in the selection are skipped.

Watch out. Creating folders on a shared network drive needs write permission there. If the macro stops with Permission denied, try a folder in your own Documents first.

On a Mac, FileDialog does not exist, so the #If Mac Then branch asks for the folder path in an input box instead (for example /Users/anna/Documents/Reports). Excel for Mac is sandboxed: the first time a macro opens files in a folder, macOS may ask you to grant access to it. Click Grant Access.

Tip. Use VBA macro to list all files in a folder in Excel afterwards to check the result, or the opposite direction: list existing folders and compare them with your list.

Related: VBA macro to list all files in a folder in Excel, VBA macro to create sheets from a list of names in Excel, Files and folders: open, save, loop through files and write text files.

More vba macros