Creating folders automatically from Excel
You keep the list of your clients, your sites or your files in Excel, and for each one you recreate the same tree on the disk by hand. Thirty seconds per folder, several times a day. Here is the basic principle in VBA, its practical limits, and why a finished solution spares you a lot of grief.
What to prepare
The principle fits in one sentence: every row of the table describes a folder, and a macro walks the rows to create the matching folders. Start with a sheet named Dossiers, built like this:
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Root folder | D:\Clients | ||
| 2 | ||||
| 3 | Client | Category | Path created | Date |
| 4 | Dupont SARL | Building | ||
| 5 | Martin & Fils | Building | ||
| 6 | Cabinet Leroy | Commercial |
Then save the workbook as .xlsm (“Excel Macro-Enabled Workbook”). An .xlsx keeps no code at all: you would write the macro and it would vanish when you closed the file. Then open the editor with
Alt + F11, and do Insert → Module.
Code placed in a sheet's object can only be called from that sheet. In a standard module, the macro appears in the
Alt + F8 list and can be attached to a button.
The VBA outline: the basic principle (~10% of the code)
The code below illustrates the elementary mechanism: read a cell in Excel and call the native MkDirinstruction. It is the theoretical embryo of the automation, and represents about 10% of the real code needed.
Option Explicit
' Minimal principle: create a single folder from a cell
Public Sub CreerDossierSimple()
Dim racine As String, nomDossier As String, chemin As String
racine = ThisWorkbook.Worksheets("Dossiers").Range("B1").Value
nomDossier = ThisWorkbook.Worksheets("Dossiers").Cells(4, 1).Value
chemin = racine & "\" & nomDossier
' Elementary creation:
If Len(Dir(chemin, vbDirectory)) = 0 Then
MkDir chemin
End If
' [ ... Rest of the code not shown: recursive creation of the subtrees,
' filtering of the 9 forbidden characters, handling of errors 76 and 52,
' 260-character check, time stamping, completion and integrity ... ]
End Sub
Why this extract is only 10% of the solution
VBA's native MkDir instruction looks attractive at first sight, but in a company's daily practice, this home-made code fails nine times out of ten :
- Instant error 76 (Path not found) :
MkDircan only create one level at a time. If you ask forD:\Clients\Building\Dupont SARLand the parent folderBuildingdoes not exist yet, the macro stops dead on a blocking error message. Handling that reliably calls for a complete recursive function that tests and walks up the tree level by level. - The 9 characters Windows forbids : a client name such as “Société A/B”, “Dupont & Associés: Site” or a quotation mark crashes the macro or creates incoherent, unwanted subfolders. You need a filtering engine to clean up every name without distorting your data.
- The risk of overwriting or stopping early : without a non-destructive integrity check, running the macro again on a modified table risks blocking on existing folders or overwriting files.
- Everyday ergonomics : having to open the VBA editor (
Alt + F11) or launch a macro throughAlt + F8is not viable for colleagues. To turn this script into a real tool, you need a dedicated Excel ribbon with buttons, icons and awareness of the active row.
For a one-off need on a single folder, the MkDir instruction is enough. But as soon as you manage dozens of client folders with structured subfolders, the complexity explodes.
The four traps of Windows paths
1. The 260-character limit
Windows has historically refused paths longer than 260 characters. With a long network root, a category, a client name and three levels of subfolders, the limit arrives sooner than you would think. Add this check before creating:
If Len(cible) > 230 Then
MsgBox "Path too long (" & Len(cible) & " characters):" & vbCrLf & cible
GoTo LigneSuivante
End If
The 30-character margin leaves room for the names of the files that will be dropped inside.
2. Trailing spaces, invisible
A name copied from another program often drags a trailing space along. Windows silently removes it when the folder is created, but VBA keeps it in memory: the folder “Dupont SARL” is created, and Dir will then look for “Dupont SARL ”. Hence the Trim$ in
NomValide.
3. Synchronised folders
On OneDrive, SharePoint or a network drive, the folder looks created from Excel's point of view while synchronisation is still under way. Writing a file straight afterwards can produce a conflict. If your root is synchronised, allow a pause after creation, or work on a local folder that you synchronise afterwards.
4. Dir keeps its state
Dir is a function with a memory: calling it with no argument continues the previous search. Nesting two Dir
loops gives false results, with no error message. If you have to walk through files, use FileSystemObject :
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(cible) Then fso.CreateFolder cible
Subfolders, standard documents, OneDrive
Three refinements make the macro genuinely useful day to day.
Let the sheet describe the tree, rather than the code. Replace the fixed sousDossiers array with a read from cells: put your levels in column H of a Settingssheet, and read them from there. You change the tree without reopening the VBA editor.
Open the folder from the row. Once the path is in column C, a double-click can open Explorer in the right place. In the sheet's module:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Column = 3 And Len(Target.Value) > 0 Then
Shell "explorer.exe """ & Target.Value & """", vbNormalFocus
Cancel = True
End If
End Sub
Drop in the starting documents. That is the next step, and the most rewarding: copying into each new folder a quote, a contract or a form already in the client's name. It is covered in the guide filling a Word document from Excel.
An .xlsm downloaded or received by e-mail arrives blocked by Windows. You have to do right-click → Properties → Unblock before you can enable macros. On a file you send to colleagues, warn them: without that gesture, the ribbon and the buttons will stay inert, with no explanation.
The complete, safe, no-code solution
The outline above gives an idea of how it works in theory. But turning that minimal script into a company tool the whole team can use takes a disproportionate investment: you need a preview before writing, backups, a resume mode when a folder already exists, handling of forbidden characters and accents, respect for OneDrive synchronisation, diagnostics when something jams — and somebody to maintain all of it.
That is exactly the scope of the DossiersLocauxadd-in. Everything is ready to use, finished and proven: a preview of the exact path before anything is written, non-destructive completion that overwrites nothing, automatic filling of the standard documents in your subfolders, all reachable straight from an ergonomic Excel ribbon.
And if you would rather not maintain it yourself
The add-in that does everything above, finished and proven: path preview before writing, completion that overwrites nothing, standard documents filled in with the client's name, updates as tracked changes when the template changes. VBA source delivered open.
Discover DossiersLocaux — €19 for life → Lifetime licence, no subscription · Refunded for 30 days · Windows + Excel 2016 or newerFrequently asked questions
Do macros have to be enabled for this to work?
Yes. The workbook must be saved as .xlsm and macros allowed when it opens. If the file comes from the internet or from an attachment, Windows also marks it as blocked: right-click the file, Properties, then tick Unblock before opening it.
Does the macro work on a Mac?
Partly. MkDir and Dir exist in Excel for Mac, but path syntax differs (colons or slashes depending on the version), Shell "explorer.exe" has no equivalent, and disk access goes through sandbox permissions. The code in this guide is written for Excel on Windows.
How do I create the folders on a network drive?
Give the full UNC path in B1, for example \\server\share\Clients, rather than a mapped drive letter: the letter can differ from one machine to the next, the UNC path is the same for everyone. Check that the Windows account has write permission.
Can folders be deleted or renamed with the same method?
Technically yes (RmDir, Name), but it is strongly discouraged in a production macro. One wrong row, and you wipe out a client's folder. The code in this guide never deletes anything: it creates what is missing and leaves the rest intact.
What happens if two rows carry the same client name?
The second pass finds the existing folder, counts it as “already there” and completes it. No duplicate is created and no file is overwritten. If you really do want two separate folders, add an identifier to the name, for example the project number.