Help Creating subfolders using a macro

hkydad

Board Regular
Joined
May 16, 2013
Messages
66
Office Version
  1. 365
Platform
  1. Windows
I have a macro that when run, creates a set of folders in a directory based on information in a spreadhseet. Now what I want to do is create sub-folders based on another column within the same spreadhseet. Here are exapmples of the code and the spreadhseet.

Sub MakeFolders_485BPOS()
Dim xdir As String
Dim fso
Dim lstrow As Long
Dim i As Long
Set fso = CreateObject("Scripting.FileSystemObject")
lstrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For i = 1 To lstrow
xdir = "Y:\Filing_Types\485BPOS\" & Range("A" & i).Value
If Not fso.FolderExists(xdir) Then
fso.CreateFolder (xdir)
End If
Next
Application.ScreenUpdating = True
End Sub

Now once these folders are created, I want to create several subfolders under each of the folders created above. How do I do this?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
HI..

Say you Root Folder is in Column A.. and your sub folder is in column D.. this will create a a Folder named with Column A value and a sub folder within that with column D value as its folder name..

Code:
Private Sub CommandButton1_Click()


Dim xdir As String
Dim fso
Dim fsoSub
Dim lstrow As Long
Dim i As Long
Dim xdirsubfolder As String
Set fso = CreateObject("Scripting.FileSystemObject")
Set fsoSub = CreateObject("Scripting.FileSystemObject")
lstrow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
Application.ScreenUpdating = False
For i = 1 To lstrow
xdir = "D:\Excel\Test\" & Range("A" & i).Value
xdirsubfolder = "D:\Excel\Test\" & Range("A" & i).Value & "\" & Range("A" & i).Offset(0, 3)


If Not fso.FolderExists(xdir) Then
fso.CreateFolder (xdir)
End If


If Not fsoSub.FolderExists(xdirsubfolder) Then
fsoSub.CreateFolder (xdirsubfolder)
End If


Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi...
Thank you for this code. It works perfectly. I even like the fact that the code ignores duplicate lines in the spreadhseet so that I only end up with one set of folders.

This is exactly what I was looking for so thank you again for your help.


:)
 
Upvote 0
Hi.. I am glad that it helped.. one minor thing.. next time when posting code.. try to use the Code tags option (available when you Select the "Go Advanced" option on your reply/post)..

:)
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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