Macro to create new folder based on previous month and move files

Alaa mg

Active Member
Joined
May 29, 2021
Messages
343
Office Version
  1. 2019
hi experts

I search for macro to create new folder and move the files for each created new folder based on previous month for PC

so every month I have many files contains purchases reports . for instance , take month MAR when the files send me , it will be 1 of APRIL and attach the files in this directory C:\Users\PC AAA\Downloads\REPORT . the macro should create new folder name is REPORT _ 3-2022 based on previous month and move the attached files in main directory in folder REPORT to folder REPORT _ 3-2022 . so if the current month on PC is APRIL should create folder is relating in previous month in PC is MAR and move all of the files are existed from folder REPORT to created new folder REPORT _ 3-2022 and so on for all of the months in current year and when finish this year and enter new year then should start again REPORT _ 1-2023 , REPORT _ 2-2023 . ...
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hopefully this resolves the problem : You can just change 'MyFolPath' and everything else as same.

VBA Code:
Sub MoveMonthlyFilesToRespectiveFolders()
Sheets.Add.Name = "FileNames"
Sheets("FileNames").Activate
MyFolPath = "C:\Users\PC AAA\Downloads\REPORT"
Dim oFSO As Object
Dim oFolder As Object
Dim oFile As Object
Dim i As Integer
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(MyFolPath)
For Each oFile In oFolder.Files
    Cells(i + 1, 1) = oFile.Name
    Cells(i + 1, 2) = FileDateTime(oFolder & "\" & oFile.Name)
    Cells(i + 1, 3) = MonthName(Month(Cells(i + 1, 2))) & "_" & Year(Cells(i + 1, 2))
    i = i + 1
Next oFile
Range("C1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Range("D1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveSheet.Range("$D$1:$D$1048576").RemoveDuplicates Columns:=1, Header:=xlNo
i = 1
Do While Cells(i, 4) <> "" 'Loop through to create folders
    MkDir MyFolPath & "\" & Cells(i, 4).Value
    i = i + 1
Loop
i = 1
Dim FSO As Object
Dim SourceFileName As String, DestinFileName As String
Set FSO = CreateObject("Scripting.Filesystemobject")
Do While Cells(i, 1) <> "" 'Loop through to move files
    SourceFileName = MyFolPath & "\" & Sheets("FileNames").Cells(i, 1).Value
    DestinFileName = MyFolPath & "\" & Cells(i, 3).Value & "\" & Sheets("FileNames").Cells(i, 1).Value
    FSO.MoveFile Source:=SourceFileName, Destination:=DestinFileName
    i = i + 1
Loop
End Sub

Let me know if you are finding any issues.
 
Upvote 0
Make sure there are no sheets named "FileNames" in your excel file.
 
Upvote 0

Forum statistics

Threads
1,214,790
Messages
6,121,608
Members
449,038
Latest member
apwr

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