Dynamic file path in VBA

Gorandulac32

New Member
Joined
Jan 9, 2020
Messages
18
Office Version
  1. 2016
Platform
  1. Windows
I have a macro that essentially takes all files in a folder path and creates a master file with each file being named as a tab on the master file macro.

For example the folder in the path below has files called 0701file, 0702file, 0703file which all get created as tabs. My issue is that i need to change the bolded 07 every month for the macro to work. I guess at year end I would run into the same same issue. My question is how do I make the file path dynamic so i can pick the year and the month, since those are the only variables that would change in the path.

Thanks

—————————————————————————————————————————————————————————————————————————


Sub MergeWorkbooks()

Dim FolderPath As String
Dim File As String
Dim i As Long


FolderPath = "C:\Users\Goran\2021\07

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
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Answer is it depends. You could have the values in a couple of cells. You could assign it to this month and this year. Depends what you need.
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,618
Members
449,092
Latest member
amyap

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