VBA to create folders for the years

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
788
Office Version
  1. 365
Please help looking for a code that can create forlders for the years like screenshot below.

1. Ask for location where to save
2. Create folders
MONTH FOLDERS FOR THE YEAR.png
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Try this code:
VBA Code:
Sub MyCreateFolders()

    Dim pickFolder As FileDialog
    Dim myPath As String
    Dim yr As Long
    Dim m As Long
    Dim fl As String
    
'   Select path/folder from dialog box
    Set pickFolder = Application.FileDialog(msoFileDialogFolderPicker)
    
    With pickFolder
        .Title = "Select A Folder"
        .AllowMultiSelect = False
        If .Show <> -1 Then Exit Sub    'Check to see if cancel button clicked
        myPath = .SelectedItems(1) & "\"
    End With
    
'   Prompt user for year to create
    On Error GoTo err_fix
    yr = InputBox("Please enter the year you would like to create folders for")
    On Error GoTo 0
    
'   Loop through all months for the year
    For m = 1 To 12
'       Build new folder name
        fl = Format(DateSerial(yr, m, 1), "mm mmm yyyy")
'       Create folder
        MkDir (myPath & fl)
    Next m
    
    MsgBox "Macro complete!"
    Exit Sub
    
err_fix:
    MsgBox "Invalid folder name", vbOKOnly, "Process Aborted!"
    
End Sub
 
Upvote 0
Solution
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,226,050
Messages
6,188,571
Members
453,484
Latest member
jlo1673

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