Mass Creation Of Folders

will2learn

Board Regular
Joined
Dec 1, 2005
Messages
144
Is it possible to create a varying amount of subfolders automatically using a macro?

The folders will be named on a sheet called 'folder names' in cells A2:A53, but this can vary. Ideally I need to be able to select or create the initial folder if it does not already exist and the subfolders to then be created in the selected folder.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
This ought to help
Code:
Sub buildFolders()
    Dim fldr As String, fso As Variant
    fldr = "c:\tempfolders\"
    Set newfolders = Range("A1:A10")
    Set fso = CreateObject("Scripting.FileSystemObject")
    If Not fso.folderexists(fldr) Then
        fso.createfolder (fldr)
    End If
    For Each f In newfolders
        fso.createfolder (fldr & f.Value)
    Next f
End Sub

In the example the main folder is defined as fldr and if it doesn't exist, will be created.
The range A1:A10 of the currently active sheet contains the list of subfolders to be created.
There's no smart error checking for duplicates or illegal file names, but this could be added if required.

HTH
 
Upvote 0
Another version with just Excel (no fso).

Code:
Sub MakeDirs()
Dim Cell As Range
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For Each Cell In Range("A2:A" & LR)
    MkDir Cell.Text
Next
End Sub

The folders should be ordered so top-level directories are created before sub folders
ie
<TABLE style="WIDTH: 130pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=173><COLGROUP><COL style="WIDTH: 130pt; mso-width-source: userset; mso-width-alt: 6326" width=173><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: transparent; WIDTH: 130pt; HEIGHT: 15pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" height=20 width=173>C:\users\owner\temp</TD></TR><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #f0f0f0; BORDER-LEFT: #f0f0f0; BACKGROUND-COLOR: transparent; HEIGHT: 15pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: #f0f0f0" height=20>C:\users\owner\temp\2011_01
C:\users\owner\temp\2011_02
</TD></TR></TBODY></TABLE>
 
Upvote 0
Hi Tweedle,

I was using your trailing code for creation of multiple folders. It works fine but however I am getting the Run time Error 75 everytime I run the code. However the folder was created. Could you please help me to sort out this error.

Code:
Sub MakeDirs()
Dim Cell As Range
Dim LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For Each Cell In Range("A2:A" & LR)
    MkDir Cell.Text
Next
End Sub

When I debug the code, its showing error at MkDir Cell.Text

Also I would like to create sub folders also in the same code. Kindly help me to sort out this issue.

Thanks and Regards
R. Vadivelan
 
Upvote 0
Something is incorrect with the paths attempted to create: Path/File access error (Error 75)

The folders should be ordered so top-level directories are created before sub folders
ie
C:\users\owner\temp
C:\users\owner\temp\2011_01
C:\users\owner\temp\2011_02
This is Win 7 pathing convention for creating a temp and subfolders within temp of my local computer user id.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,673
Members
452,937
Latest member
Bhg1984

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top