Creating Windows Folders and Subfolders VBA Code Modification Help

bellownc

New Member
Joined
Dec 23, 2020
Messages
3
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I found this post from earlier this year. I used it to create a folder and subfolder structure and it appeared to work well but was only able to go one subfolder level deep. In the below data set folders from Column B and folders for Column C were all created as subfolders of Column A. I want Column C to be a subfolder of Column B,

My format is :
Column AColumn BColumn C
FOLDER 1folder 1.1folder 1.1.1
FOLDER 1folder 1.1folder 1.1.2
FOLDER 1folder 1.2folder 1.2.1
FOLDER 1folder 1.2folder 1.2.2
FOLDER 2folder 2.1folder 2.1.1
FOLDER 2folder 2.2folder 2.2.1

to create a file structure in windows that looks like this:

1608744587889.png


I used this code:

VBA Code:
Public Sub CreateFolderStructure2()

    Dim baseFolder As String
    Dim objRow As Range, c As Long
 
    baseFolder = "C:\path\to\base folder\"
    If Right(baseFolder, 1) <> "\" Then baseFolder = baseFolder & "\"
 
    For Each objRow In Worksheets("Sheet2").UsedRange.Rows
        For c = 2 To objRow.Cells.Count
            Shell "cmd /c md " & Chr(34) & baseFolder & objRow.Cells(1, 1) & "\" & objRow.Cells(1, c) & Chr(34)
        Next
    Next
 
End Sub

I would like to modify the above code to meet my needs but I am pretty new to this type of coding.
Any help that could be provided would be great.
 
Last edited by a moderator:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub bellownc()
   Dim Pth As String, FullPth As String
   Dim Cl As Range
   Dim i As Long
   
   Pth = "C:\MrExcel\"
   If Right(Pth, 1) <> "\" Then Pth = Pth & "\"
   With Sheets("Sheet2")
      For Each Cl In .Range("A2", .Range("A" & Rows.count).End(xlUp))
         FullPth = Pth & Cl.Value & "\"
         If Not FldrExists(FullPth) Then MkDir FullPth
         For i = 1 To .Cells(Cl.Row, Columns.count).End(xlToLeft).Column - 1
            FullPth = FullPth & Cl.Offset(, i).Value & "\"
            If Not FldrExists(FullPth) Then MkDir FullPth
         Next i
         FullPth = Pth
      Next Cl
   End With
End Sub
Function FldrExists(DirPth As String) As Boolean
   FldrExists = Dir(DirPth, vbDirectory) <> ""
End Function
 
Upvote 0
Solution
Hi & welcome to MrExcel.
How about
VBA Code:
Sub bellownc()
   Dim Pth As String, FullPth As String
   Dim Cl As Range
   Dim i As Long
  
   Pth = "C:\MrExcel\"
   If Right(Pth, 1) <> "\" Then Pth = Pth & "\"
   With Sheets("Sheet2")
      For Each Cl In .Range("A2", .Range("A" & Rows.count).End(xlUp))
         FullPth = Pth & Cl.Value & "\"
         If Not FldrExists(FullPth) Then MkDir FullPth
         For i = 1 To .Cells(Cl.Row, Columns.count).End(xlToLeft).Column - 1
            FullPth = FullPth & Cl.Offset(, i).Value & "\"
            If Not FldrExists(FullPth) Then MkDir FullPth
         Next i
         FullPth = Pth
      Next Cl
   End With
End Sub
Function FldrExists(DirPth As String) As Boolean
   FldrExists = Dir(DirPth, vbDirectory) <> ""
End Function
Thanks. I will try this out and let you know. Appreciate it.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,125
Members
453,021
Latest member
Justyna P

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