I am trying to find a proper VBA code to create a batch folders within the folder where the excel file with the code sits based on multiple cell data (columns and rows)
I have have came across this code
Sub CreateFolders()
'Variable definations
Dim FolderListRange As Range
Dim FolderRange As Variant
Dim FolderName As String
Dim ParentFolderPath As String
On Error GoTo Handle
' Set the Folder where the individual folders should be created
ParentFolderPath = "Folders"
Set FolderListRange = ActiveSheet.Range("A2:A64000").SpecialCells(xlCellTypeConstants)
For Each FolderRange In FolderListRange
If FolderRange.Offset(0, 1).Value = "" Then GoTo Continue
FolderName = ActiveWorkbook.Path & "\" & FolderRange.Value & "-" & Format(FolderRange.Offset(0, 1).Value, "dd-mm-yyyy")
If FileSystem.Dir(FolderName, vbDirectory) = vbNullString Then
FileSystem.MkDir FolderName
End If
Continue:
Next
But this one only creates folders from one column in this case A and down. What I need is folders to be created based on values from A2:C2, A3:C3, etc.
Header of the columns are
A1 B1 C1
No. Reg MSN
1 XXX 21334
2 xxy 576576
So folder structure after createion should be
1-XXX-21334
2-XXY-576576
Any help would be highly appreciated. Cheers
I went the other way.
I have created a preview column with the following "=CONCATENATE(A2," ",C2," ",B2,"_",D2)"
and then used the following code to create the folders.
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
Tnx for help.
I additionally added Play1
and Play2
in column C
and left column D
empty to get the following folders:
C:\Test\Folder A
C:\Test\Folder B
C:\Test\Folder C
C:\Test\Folder A\Sub1
C:\Test\Folder A\Sub2
C:\Test\Folder A\Sub3
C:\Test\Folder B\Sub1
C:\Test\Folder B\Sub2
C:\Test\Folder B\Sub3
C:\Test\Folder C\Sub1
C:\Test\Folder C\Sub2
C:\Test\Folder C\Sub3
C:\Test\Folder A\Sub1\Play1
C:\Test\Folder A\Sub1\Play2
C:\Test\Folder A\Sub2\Play1
C:\Test\Folder A\Sub2\Play2
C:\Test\Folder A\Sub3\Play1
C:\Test\Folder A\Sub3\Play2
C:\Test\Folder B\Sub1\Play1
C:\Test\Folder B\Sub1\Play2
C:\Test\Folder B\Sub2\Play1
C:\Test\Folder B\Sub2\Play2
C:\Test\Folder B\Sub3\Play1
C:\Test\Folder B\Sub3\Play2
C:\Test\Folder C\Sub1\Play1
C:\Test\Folder C\Sub1\Play2
C:\Test\Folder C\Sub2\Play1
C:\Test\Folder C\Sub2\Play2
C:\Test\Folder C\Sub3\Play1
C:\Test\Folder C\Sub3\Play2
The Code
Option Explicit
Sub createFolders()
Const FolderPath As String = "C:\Test"
Const wsName As String = "Sheet1"
Const FirstRow As Long = 2
Dim Cols As Variant: Cols = Array(1, 2, 3, 4)
Dim Paths As Variant: ReDim Paths(0): Paths(0) = FolderPath
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
Dim Data As Variant: ReDim Data(UBound(Cols))
Dim j As Long
For j = 0 To UBound(Cols)
getColumn Data(j), ws, Cols(j), FirstRow
Next j
For j = 0 To UBound(Cols)
If IsArray(Data(j)) Then
createPaths(Paths, Data(j))
End If
Next j
End Sub
Sub createPaths(ByRef Paths As Variant, Data As Variant)
Dim NewPaths As Variant, i As Long, j As Long, k As Long
ReDim NewPaths((UBound(Paths) + 1) * UBound(Data) - 1)
For j = 0 To UBound(Paths)
For i = 1 To UBound(Data)
NewPaths(k) = Paths(j) & Application.PathSeparator & Data(i, 1)
' Debug.Print NewPaths(k)
MkDir NewPaths(k)
k = k + 1
Next i
Next j
Paths = NewPaths
End Sub
Sub getColumn(ByRef Data As Variant, _
Sheet As Worksheet, _
Optional ByVal aColumn As Variant = 1, _
Optional ByVal FirstRow As Long = 1)
Dim rng As Range
Set rng = Sheet.Columns(aColumn).Find("*", , xlValues, , , xlPrevious)
If rng Is Nothing Then Exit Sub
If rng.Row < FirstRow Then Exit Sub
If rng.Row > FirstRow Then
Data = Sheet.Range(Sheet.Cells(FirstRow, aColumn), rng)
Else
ReDim Data(1 To 1, 1 To 1): Data(1, 1) = rng.Value
End If
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.