0

firstly sorry if my english is not perfect as i'm french.

i'm new to this and i'm trying to make something work for my company to save some time.

i'd like to make a VBA code that Create a Folder with 9 sub folder inside but the tricky part i guess is that inside those 9 sub folders i need to have again sub folders.

i then need to auto copy an excel file to the main folder that have the same name

The Main folder name need to be based after 3 Excel row "A2" "C2" "B2"

Below a screenshot of inside the Main Folder :

enter image description here

I have some code that i found on the web that does some stuff that i need but i don't know how to do it.

Below code that i have :

Sub CreateDirs()
Dim r As Range
Dim RootFolder As String
RootFolder = Range("K2").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("L2").Value
MkDir RootFolder & "\" & r.Text & "\" & Range("L3").Value
MkDir RootFolder & "\" & r.Text & "\" & Range("L4").Value
On Error GoTo 0
End If
Next r
End Sub

combined with this 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, "H").End(xlUp).Row
For X = 2 To lr
src = Range("F" & X).Value
'Destination directory
dst = Range("G" & X).Value
'Filename
fl = Range("E" & 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

Those 2 code will Create Folders with sub folders and will copy an excel file in the main folder but the Main folder is based only on one column

with this code i have this Excel Sheet :

enter image description here

I have this third code that will only create a Main Folder but with my name based on my 3 Column :

Sub ExampleCode()
Dim strName As String
Dim strCode As String
Dim strCode1 As String
Dim fName As String
Dim fPath As String
Dim lastRow As Long
Dim i As Long
Dim ws As Worksheet
    
'Where will we create these folders?
fPath = "C:\Users\WBRICET\Documents\TESTVBA"
    
'Error check
If Right(fPath, 1) <> Application.PathSeparator Then
fPath = fPath & Application.PathSeparator
End If
    
'What sheet has the data?
Set ws = ActiveSheet
    
Application.ScreenUpdating = False
With ws
'How much data do we have
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    
'Loop from row 3 to end
For i = 3 To lastRow
'Get the bits
strName = .Cells(i, "A").Value
strCode = .Cells(i, "C").Value
strCode1 = .Cells(i, "B").Value
'Build folder name
fName = strName & " " & strCode & " " & strCode1
            
'Check if folder already exists
If Dir(fPath & fName, vbDirectory) = "" Then
'Create folder
MkDir fPath & fName
End If
            
Next i
End With
    
Application.ScreenUpdating = True
    
'Since nothing changed on sheet, provide feedback to user
MsgBox "Done!"
End Sub

Sorry for my long post but that would help me a lot in my work and my coworker too.

Thanks again :)

EDIT : if it's easier for the subfolders part could we not do something that copy existing exemple empty folders to the new one created ?

Like The VBA will Create a Folder based on 3 Column "XXX XXX XXX" copy the excel in main folder and also copy empty entire Subfolder "shell" that i normaly copy by hand"

PeterT
  • 8,232
  • 1
  • 17
  • 38
odKINS
  • 1
  • 2
  • Not directly related to your problem, but it's recommended to [avoid using `Select`](https://stackoverflow.com/a/10717999/4717755) and always [qualify your references](https://riptutorial.com/excel-vba/example/5110/qualifying-references). These tips will save you some trouble as your workbook code grows. – PeterT Oct 21 '22 at 16:22
  • You have many actions: creating folders, creating subfolders, copying files, ... Can you ask a question limited to just one action? – dcromley Oct 21 '22 at 16:41
  • Have you tried a web search for "vba create subfolders recursively"? – Stefan Wuebbe Oct 22 '22 at 04:43
  • @dcromley the problem is that i need something that does all of that – odKINS Oct 26 '22 at 09:49
  • I just discovered the link https://stackoverflow.com/help/minimal-reproducible-example . Maybe start with just a little of "all of that". – dcromley Oct 26 '22 at 14:37

0 Answers0