Creating multiple child folders under a parent folder using VBA

coetzej9

New Member
Joined
Mar 4, 2016
Messages
5
Hi all,

I would like to create folders from a list. Each item in Col A needs to be a parent folder and Col B and Col C need to be the child folders for each.

Example:

Excel 2010
A
B
C
1
FranceParisNice
2
UKLondonManchester
3
ItalyMilanNaples
Sheet: Sheet2


I tried to copy and past code from post http://www.mrexcel.com/forum/excel-questions/872763-create-folder-subfolders-cell.html but get Run-time error 424.

The code I used:

Code:
Sub CreateFolderStructure()
For Each objRow In UsedRange.Rows
        strFolders = "C:\The_NEWS\"
        For Each ObjCell In objRow.Cells
            strFolders = strFolders & "\" & ObjCell
        Next
        Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
    Next
End Sub



......I would like to get the following results:

C:\The_NEWS\France\Paris\Nice\Marseille\Lyon\Nantes
C:\The_NEWS\Desktop\Italy\Milan\Naples\Rome\Turin\Genoa
C:\The_NEWS\UK\London\Manchester\Liverpool\Oxford\Preston

I have not been able to find a post addressing the copy and past error that return Run-time error 424 - Object Required.

Since I am new at VBA I am not sure if I did something wrong. I created a new module and pasted the code, changed the folder location in the code and tried to run the code.

Your assistance or pointing in the right direction would be much appreciated.

:):)
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
I've tweaked your code:
Code:
Sub CreateFolderStructure()
    Dim objRow As Range, objCell As Range, strFolders As String
    
    For Each objRow In Worksheets("Sheet2").UsedRange.Rows
        strFolders = "C:\The_NEWS"
        For Each objCell In objRow.Cells
            strFolders = strFolders & "\" & objCell.Value
        Next
        Shell "cmd /c md " & Chr(34) & strFolders & Chr(34)
    Next
End Sub
 
Upvote 0
20200217 Dummy K1 Advisor List.xlsx
ABCDE
1FranceParisMarseilleLyonToulouse
2UKLondonBirminghamCambridgeCanterbury
3ItalyMilanRomeNaplesTurin
File Names


How would the code above need to be adjusted if I wanted the country to be the main folder and all the cities would be subfolders within it, instead of all nested inside each other?
The result would be something like this:
1582768282458.png
 
Upvote 0
Change baseFolder string as required.
VBA Code:
Public Sub CreateFolderStructure2()

    Dim baseFolder As String
    Dim objRow As Range, c As Long
  
    baseFolder = "C:\path\to\base folder\"
    If Right(baseFolder, 1) <> "\" Then baseFolder = baseFolder & "\"
  
    For Each objRow In Worksheets("Sheet2").UsedRange.Rows
        For c = 2 To objRow.Cells.Count
            Shell "cmd /c md " & Chr(34) & baseFolder & objRow.Cells(1, 1) & "\" & objRow.Cells(1, c) & Chr(34)
        Next
    Next
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,427
Messages
6,124,831
Members
449,190
Latest member
rscraig11

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