Create folder, subfolders and move files into folder and subfolders

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
929
Office Version
  1. 365
Platform
  1. Windows
I have the below code that will create folders based on a selected range of folder names in a column but I want to take this further and do not know where to begin.
First I need this modified that it will create folders in a directory of your choosing NOT where the spreadsheet is located.
It will make all the folder names listed in column A in the directory listed in B. Then it will make 2 Subfolders: Documents and Drawing in each primary folder and copy documents into those sub folders (or where ever it is specified to save it).

I hope I am explaining things properly and I hope someone can help me modify the below code or if anyone knows of a better one?

I am still new to this and the users who want to use this file want it to be customizable so the details are in the below sheet.


VBA Code:
Sub MakeFolders()
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

Book3
ABCDEFGHI
1FolderFolder LocationSub Folder 1Sub Folder 2Copy Document File to LocationCopy Map File to LocationDirectory of Draft Document File:Directory of Draft Map File:
2AXX4522\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4522\Documents\\Usa-files\Quality\AXX4522\Drawing\\Usa-files\Quality\2021 - Fall\Document Draft.docx\\usa-files\Quality\Quality Assurance\Drafts\Map Draft.xlsx
3AXX4523\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4523\Documents\\Usa-files\Quality\AXX4523\Drawing
4AXX4524\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4524\Documents\\Usa-files\Quality\AXX4524\Drawing
5AXX4525\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4525\Documents\\Usa-files\Quality\AXX4525\Drawing
6AXX4526\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4526\Documents\\Usa-files\Quality\AXX4526\Drawing
7AXX4527\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4527\Documents\\Usa-files\Quality\AXX4527\Drawing
8AXX4528\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4528\Documents\\Usa-files\Quality\AXX4528\Drawing
9AXX4529\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4529\Documents\\Usa-files\Quality\AXX4529\Drawing
10AXX4530\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4530\Documents\\Usa-files\Quality\AXX4530\Drawing
11AXX4531\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4531\Documents\\Usa-files\Quality\AXX4531\Drawing
12AXX4532\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4532\Documents\\Usa-files\Quality\AXX4532\Drawing
13AXX4533\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4533\Documents\\Usa-files\Quality\AXX4533\Drawing
14AXX4534\\Usa-files\Quality\DocumentsDrawing\\Usa-files\Quality\AXX4534\Documents\\Usa-files\Quality\AXX4534\Drawing
Sheet1
Cell Formulas
RangeFormula
E2:E14E2="\\Usa-files\Quality\"&A2&"\"&C2
F2:F14F2="\\Usa-files\Quality\"&A2&"\"&D2
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Then it will make 2 Subfolders: Documents and Drawing in each primary folder and copy documents into those sub folders
Which documents : those in H2:I2 ? Both to each subfolders ?​
 
Upvote 0

As under a VBA procedure columns E & F are useless : do you want to keep them or they can be removed ?​
 
Upvote 0
I was able to figure it out using the below set up:

Folder-Sub Folder and File Copy Macro.xlsm
ABCDEFGHIJKLM
1FoldersFile NameFile SourceFile DestinationSub Folder NameRoot FolderSub Folder Name (MAX 2 Sub Folders)Sub Folder Name for File DestinationFile NameFile Source
2Folder1Blank Form.xlsxC:\Test\DocumentsC:\Test\Folder1\Name1Name1C:\TestName1Name1Blank Form.xlsxC:\Test\Documents
3Folder2Blank Form.xlsxC:\Test\DocumentsC:\Test\Folder2\Name1Name1Name2
4Folder3Blank Form.xlsxC:\Test\DocumentsC:\Test\Folder3\Name1Name1Name3
5Folder4Blank Form.xlsxC:\Test\DocumentsC:\Test\Folder4\Name1Name1
Macro
Cell Formulas
RangeFormula
B2:B5B2=$L$2
C2:C5C2=$M$2
D2:D5D2=$H$2&"\"&A2&"\"&$K$2
E2:E5E2=$K$2


The first button (vba code) creates the folders and subfolders:

VBA Code:
Sub CreateDirs()
    Dim r As Range
    Dim RootFolder As String
    RootFolder = Range("H2").Value
Range("A2").Select

    For Each r In Range(Selection, Selection.End(xlDown))
        If Len(r.Text) > 0 Then
            On Error Resume Next
            MkDir RootFolder & "\" & r.Text
            MkDir RootFolder & "\" & r.Text & "\" & Range("I2").Value
            MkDir RootFolder & "\" & r.Text & "\" & Range("I3").Value
            MkDir RootFolder & "\" & r.Text & "\" & Range("I4").Value
            On Error GoTo 0
        End If
Next r


End Sub

The Second button (VBA code) allows me to transfer the files to the folders and subfolders, or just the main folders if I leave the subfolders blank.

VBA Code:
Sub File_Transfer()
'

Dim src As String, dst As String, fl As String
Dim lr As Long
'Source directory
'Range("A2").Select
lr = Cells(Rows.Count, "C").End(xlUp).Row
For X = 2 To lr
src = Range("C" & X).Value
'Destination directory
dst = Range("D" & X).Value
'Filename
fl = Range("B" & X).Value
On Error Resume Next
'get project id
FileCopy src & "\" & fl, dst & "\" & fl
If Err.Number <> 0 Then


End If
Next X
On Error GoTo 0
End Sub

Hope this helps someone out there and thank you to all who responded/attempted to help me when I got stuck. You are all awesome and it is much appreciated!
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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