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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,620
Office Version
  1. 2007
Platform
  1. Windows
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,620
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

Yaya1900

New Member
Joined
Mar 9, 2021
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web

ADVERTISEMENT

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"
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,620
Office Version
  1. 2007
Platform
  1. Windows
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
 

Yaya1900

New Member
Joined
Mar 9, 2021
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
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 :)
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,620
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,568
Messages
5,637,093
Members
416,957
Latest member
Brovashift

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
Top