Create Folders and Sub Folders

shyoung192

New Member
Joined
Dec 27, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I used this VBA code to create 2022 folders flawlessly, now when trying to create 2023 folders I am getting an error message...
Runtime Error '5'
Invalid Procedure call or Argument
Debug is on the "Shell ("cmd/c md"&... )) Line
It is asking me to put in a Folder name and I can't remember the format I used...
Any assistance would be greatly appreciated...

Sub FolderCreator()

Dim objRow As Range, objCell As Range, strFolders As String, rootFolder As String

With Application.FileDialog(msoFileDialogFolderPicker)
' show the file picker dialog box
If .Show <> 0 Then
rootFolder = .SelectedItems(1)
End If
End With

For Each objRow In ActiveSheet.UsedRange.Rows
strFolders = rootFolder
For Each objCell In objRow.Cells
strFolders = strFolders & "\" & objCell
Next
Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
Next

End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Your macro is still in working conditions. All you need is a list of (new) folder names in an active sheet (no other data in this sheet). Then launch the macro and it will ask for the root folder.
 
Upvote 0
Your macro is still in working conditions. All you need is a list of (new) folder names in an active sheet (no other data in this sheet). Then launch the macro and it will ask for the root folder.
Thank you for checking. So, when it ask me for the FOLDER name, do I put the full path, i.e. C:\Users\Desktop\Payroll Testing\2023 or (?) because it is not working for me and I know I am doing something wrong?
 
Upvote 0
No, not necessary, the full path will be detected by the macro when it asks you to browse to the root folder.
As per your example: since you already have a folder named Payroll Testing on your desktop (your root folder), write 2023 in cell A1 of an empty sheet and launch your macro from there. If you have to create other subfolders in your new 2023 folder just delete cell A1, write down, starting from A1, all the names of new folders to be created, launch your macro and now browse to the newly create folder 2023. That's all.
 
Upvote 0
Solution
No, not necessary, the full path will be detected by the macro when it asks you to browse to the root folder.
As per your example: since you already have a folder named Payroll Testing on your desktop (your root folder), write 2023 in cell A1 of an empty sheet and launch your macro from there. If you have to create other subfolders in your new 2023 folder just delete cell A1, write down, starting from A1, all the names of new folders to be created, launch your macro and now browse to the newly create folder 2023. That's all.
rollis13...YOU'RE THE BEST...Thank you and I APPRECIATE your assistance!!!
 
Upvote 0
To create any folder and subfolders at once you can write:
2023\test1
or even:
2023\test1\test2

Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,224
Members
452,620
Latest member
dsubash

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