VBA & Macro Problems

How to Create Multiple Subfolders from Excel File Paths Using VBA

Tauseeq MagsiTauseeq Magsi Nov 2, 2026 869 views

Question details

The user needs a method to automatically generate a standard set of subfolders within multiple directories using folder paths listed in an Excel worksheet.

How to Create Multiple Subfolders from Excel File Paths Using VBA
Product
Excel
Device & OS
not provided
Scenario
Organizing project files by automatically generating specific subfolders (e.g., Sub1, Sub2, Sub3) for a large list of directory paths contained in an Excel spreadsheet.
Observed behavior
Currently, subfolders would need to be created manually, but the desired state is to execute a VBA macro that reads the paths from a specific column, skips empty cells, and creates the folders automatically.
Before you start

Ensure that all the parent folder paths listed in your Excel worksheet already exist on your computer, as the standard VBA MkDir function cannot create parent and subdirectories simultaneously.

Solution 1Recommended

Use a VBA Macro with the MkDir Function

Create a VBA script that loops through the folder paths in your designated column, ignores blank cells, and uses the MkDir command to generate the required subfolders.

The MkDir (Make Directory) statement is a built-in VBA function used to create new folders or directories. By combining this function with a For Each loop, you can quickly read a list of base paths from a worksheet and append subfolder names like "\Sub1", "\Sub2", etc.

This method is highly efficient for batch folder creation, saving hours of manual directory structuring.

1
Open the VBA Editor

Press Alt + F11 on your keyboard while in your Excel workbook to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the VBA editor, click on 'Insert' in the top menu and select 'Module' to create a blank workspace for your macro code.

3
Write the MkDir Loop Script

Enter a VBA script that defines your target range (e.g., A2 to the last used row) and loops through each cell. Use statements like MkDir cell.Value & "\Sub1", followed by MkDir cell.Value & "\Sub2" for as many subfolders as you need.

4
Add Logic to Skip Blank Cells

Wrap your MkDir commands inside an If statement, such as If cell.Value <> "" Then, to ensure the macro skips empty rows and avoids returning an error.

5
Run the Macro

Press F5 or click the green 'Run' button on the toolbar to execute the script and instantly generate the subfolders inside your listed directories.

Use a VBA Macro with the MkDir Function
Error Handling Recommendation: To prevent the macro from crashing if a subfolder already exists, you can add 'On Error Resume Next' at the beginning of your loop and 'On Error GoTo 0' at the end.
WPS Spreadsheet Automation

Run VBA Macros Seamlessly in WPS Office

WPS Office provides excellent built-in support for VBA and Macros, allowing you to run scripts to automate tasks like batch folder creation directly from your spreadsheets. It offers a lightweight, high-performance environment that is fully compatible with Microsoft Excel.

  1. 1. Open Your File in WPS Spreadsheets: Launch WPS Office and open your .xlsm or .xlsx file containing the list of folder paths.
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon menu and click 'VBA Editor' to open the macro scripting environment.
  3. 3. Execute Your MkDir Macro: Paste your subfolder creation script into a new module and hit 'Run' to process the paths and create your folders instantly.
Fully compatible with Microsoft Excel formats (.xlsx, .xlsm, .xls) and standard VBA scripts.Built-in Macro editor enables easy code writing, editing, and debugging without extra installations.Lightweight software footprint ensures fast processing speeds, even when manipulating thousands of rows.Free and intuitive Office suite alternative with a familiar user interface for smooth migration.
microsoft office alternative - wps office

Frequently Asked Questions

What happens if the parent folder doesn't exist when running the VBA MkDir command?

If the base directory path specified in your Excel cell does not exist, the MkDir function will trigger a 'Path not found' runtime error (Error 76). You must ensure all parent directories exist beforehand, or utilize a more advanced custom function to build nested directories layer by layer.

How can I prevent the macro from crashing if a subfolder already exists?

You can add error handling by placing 'On Error Resume Next' before your MkDir commands, and 'On Error GoTo 0' afterward. This will instruct the macro to bypass the 'File/Path access error' (Error 75) if the subfolder is already present.

Can I use VBA to create subfolders based on dynamic cell names instead of fixed names like 'Sub1'?

Yes, you can concatenate the path with the value of another cell instead of hardcoding a string. For example, instead of MkDir cell.Value & "\Sub1", you can use MkDir cell.Value & "\" & cell.Offset(0, 1).Value to name the subfolder dynamically based on the text in the adjacent column.