Gorandulac32
New Member
- Joined
- Jan 9, 2020
- Messages
- 18
- Office Version
- 2016
- Platform
- Windows
So the below code works to go into a file path C:\Users\Goran\2021\06 and takes all the files from that file path and combines them into 1 file and the contents of each file are a separate tab. For example 0601file, 0602file from the folder get created as a tab on the master file. The question is how do i dynamically account for the month change. The code I have below works, but i would need to change the date manually each time and then run the macro.
Sub MergeWorkbooks()
Dim FolderPath As String
Dim File As String
Dim i As Long
FolderPath = "C:\Users\Goran\2021\06
File = Dir(FolderPath)
Do While File <> ""
Workbooks.Open FolderPath & File
ActiveWorkbook.Worksheets(1).Copy _
after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
ActiveSheet.Name = Replace(File, ".xlsx", "")
Workbooks(File).Close
File = Dir()
Loop
For i = 1 To 12
Worksheets(MonthName(i, True)).Move after:=Worksheets(Worksheets.Count)
Next
End Sub
Sub MergeWorkbooks()
Dim FolderPath As String
Dim File As String
Dim i As Long
FolderPath = "C:\Users\Goran\2021\06
File = Dir(FolderPath)
Do While File <> ""
Workbooks.Open FolderPath & File
ActiveWorkbook.Worksheets(1).Copy _
after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
ActiveSheet.Name = Replace(File, ".xlsx", "")
Workbooks(File).Close
File = Dir()
Loop
For i = 1 To 12
Worksheets(MonthName(i, True)).Move after:=Worksheets(Worksheets.Count)
Next
End Sub