Creating folders and subfolders from cell value

dfstoepfel

New Member
Joined
Jul 18, 2014
Messages
10
Hello all,

I've searched and searched and can't seem to find a solution, at least not one I can understand. I have a spreadsheet with a list of names and what I'd like to do is have a macro create a folder and a few subfolders for each name I highlight. I found the macro below, that does half of what I want it to do. If I save the spreadsheet in the folder where I want the new folders created, then highlight the cells with the names I want and run the macro, I'll get a list of folders properly named from the highlighted cells. That part is great, however I'd also like to have 5 subfolders created within each new folder, all at the same level. All of the subfolders will be the same each time, for example (apple, pear, orange, banana, lemon) in each named folder. So I can either enter them somewhere on the spreadsheet so the macro can grab them or if they can just be in the macro code, that would be great. Any help would be appreciated.

Sub CreateFolders()

'create the folders where-ever the workbook is saved

Dim Rng As Range

Dim maxRows, maxCols, r, c As Integer

Set Rng = Selection

maxRows = Rng.Rows.Count

maxCols = Rng.Columns.Count

For c = 1 To maxCols

r = 1

Do While r <= maxRows

If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then

MkDir (ActiveWorkbook.Path & "\" & Rng(r, c))

On Error Resume Next

End If

r = r + 1

Loop

Next c

End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try this:

VBA Code:
Sub CreateFolders()
  'create the folders where-ever the workbook is saved
  Dim c As Range, fld As String, a As Variant
  
  For Each c In Selection
    fld = ThisWorkbook.Path & "\" & c
    If Dir(fld, vbDirectory) = "" Then MkDir fld
    For Each a In Array("apple", "pear", "orange", "banana", "lemon")
      If Dir(fld & "\" & a, vbDirectory) = "" Then MkDir fld & "\" & a
    Next
  Next c
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
Try this:

VBA Code:
Sub CreateFolders()
  'create the folders where-ever the workbook is saved
  Dim c As Range, fld As String, a As Variant
 
  For Each c In Selection
    fld = ThisWorkbook.Path & "\" & c
    If Dir(fld, vbDirectory) = "" Then MkDir fld
    For Each a In Array("apple", "pear", "orange", "banana", "lemon")
      If Dir(fld & "\" & a, vbDirectory) = "" Then MkDir fld & "\" & a
    Next
  Next c
End Sub
Hi Dante, thank you for the code. Could you kindly add one more line in your code to add two folders under each subfolder as follows: "Apple" > "20210309" & "20210308"
 
Upvote 0
Hi Dante, thank you for the code. Could you kindly add one more line in your code to add two folders under each subfolder as follows: "Apple" > "20210309" & "20210308"
Try this:

VBA Code:
Sub CreateFolders()
  'create the folders where-ever the workbook is saved
  Dim c As Range, fld As String, a As Variant
  
  For Each c In Selection
    fld = ThisWorkbook.Path & "\" & c
    If Dir(fld, vbDirectory) = "" Then MkDir fld
    For Each a In Array("apple", "pear", "orange", "banana", "lemon")
      If Dir(fld & "\" & a, vbDirectory) = "" Then MkDir fld & "\" & a
      If Dir(fld & "\" & a & "\" & "20210309", vbDirectory) = "" Then MkDir fld & "\" & a & "\" & "20210309"
      If Dir(fld & "\" & a & "\" & "20210308", vbDirectory) = "" Then MkDir fld & "\" & a & "\" & "20210308"
    Next
  Next c
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub CreateFolders()
  'create the folders where-ever the workbook is saved
  Dim c As Range, fld As String, a As Variant
 
  For Each c In Selection
    fld = ThisWorkbook.Path & "\" & c
    If Dir(fld, vbDirectory) = "" Then MkDir fld
    For Each a In Array("apple", "pear", "orange", "banana", "lemon")
      If Dir(fld & "\" & a, vbDirectory) = "" Then MkDir fld & "\" & a
      If Dir(fld & "\" & a & "\" & "20210309", vbDirectory) = "" Then MkDir fld & "\" & a & "\" & "20210309"
      If Dir(fld & "\" & a & "\" & "20210308", vbDirectory) = "" Then MkDir fld & "\" & a & "\" & "20210308"
    Next
  Next c
End Sub
Thank you so much :)
 
Upvote 0

Forum statistics

Threads
1,213,560
Messages
6,114,306
Members
448,564
Latest member
ED38

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