Create folders based on column range

jmazorra

Well-known Member
Joined
Mar 19, 2011
Messages
715
Hello folks:

I created a script to create a folder based on a cell value. However, I actually needed to create folders based on the values of column F. How do I create a loop to do that?

Any help will be appreciated.


VBA Code:
Sub CreateFolder()
Dim NewFolder As String
    NewFolder = "C:\2022 STI Folder\PDF\" & Range("F2")
        If Len(Dir(NewFolder, vbDirectory)) = 0 Then
           MkDir NewFolder
        Else
            MsgBox "Folder already exists"
        End If
End Sub
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try this:
VBA Code:
Sub CreateFolder()

Dim lr As Long
Dim r As Long
Dim NewFolder As String

'   Find last row in column F with data
    lr = Cells(Rows.Count, "F").End(xlUp).Row
    
'   Loop through all rows starting on row 2
    For r = 2 To lr
        NewFolder = "C:\2022 STI Folder\PDF\" & Range("F" & r)
        If Len(Dir(NewFolder, vbDirectory)) = 0 Then
           MkDir NewFolder
        Else
            MsgBox "Folder already exists"
        End If
    Next r
    
End Sub
 
Upvote 0
Try this:
VBA Code:
Sub CreateFolder()

Dim lr As Long
Dim r As Long
Dim NewFolder As String

'   Find last row in column F with data
    lr = Cells(Rows.Count, "F").End(xlUp).Row
   
'   Loop through all rows starting on row 2
    For r = 2 To lr
        NewFolder = "C:\2022 STI Folder\PDF\" & Range("F" & r)
        If Len(Dir(NewFolder, vbDirectory)) = 0 Then
           MkDir NewFolder
        Else
            MsgBox "Folder already exists"
        End If
    Next r
   
End Sub
quick question, what can I do if instead of the msgbox I want the script to ignore the already created folder and move to the creating the next folder?
 
Upvote 0
Just remove the "Else" clause altogether, i.e.
VBA Code:
Sub CreateFolder()

Dim lr As Long
Dim r As Long
Dim NewFolder As String

'   Find last row in column F with data
    lr = Cells(Rows.Count, "F").End(xlUp).Row
   
'   Loop through all rows starting on row 2
    For r = 2 To lr
        NewFolder = "C:\2022 STI Folder\PDF\" & Range("F" & r)
        If Len(Dir(NewFolder, vbDirectory)) = 0 Then
           MkDir NewFolder
        End If
    Next r
   
End Sub
 
Upvote 0
Solution
Sub CreateFolder() Dim lr As Long Dim r As Long Dim NewFolder As String ' Find last row in column F with data lr = Cells(Rows.Count, "F").End(xlUp).Row ' Loop through all rows starting on row 2 For r = 2 To lr NewFolder = "C:\2022 STI Folder\PDF\" & Range("F" & r) If Len(Dir(NewFolder, vbDirectory)) = 0 Then MkDir NewFolder End If Next r End Sub
Thank you, exactly what I needed
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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