Copying data in daily monthly files into one consolidated template or file.

iP_123

Board Regular
Joined
Apr 18, 2016
Messages
99
Hi Team.

Perhaps this silly question has been asked and resolved once before but I could not find it anywhere but nevertheless I will appreciate your help as always. I am trying to figure out an efficient way to write a vba to copy all data from within daily files in a folder (the daily files will begin with 01 for January and 02 for February and etcetera) into a new template file. The daily files reside in two seperate folders but the vba copies all files with 01 in the file name into the template.
The goal is to create monthly consolidated data out of the daily files. I will essentially run this vba once a month to consolidate all the data in the daily files into one template. So next month, I will run the vba for all files in both folders that has 10 in the file name for October.

Any help will greatly be appreciated.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
I would do this with Power Query. Get Data from a Folder. Use a parameter to select the particular month or filter it directly in PQ.

 
Upvote 0
Thank you alansidman and very much appreciated however I am encountering errors so I did some more research and I was able to find the below vba which I tried repurposing. The help I need to make this work is not to specify a range in the vba as the number of row data varies from file to file.
Is there a way to set the range object to be all the data in columns a through Y in each file and not just 300 as seen in the code below?

Sub Copy_Range_From_Workbooks()

Dim fileCells As Range, fileCell As Range
Dim destCells As Range, r As Long
Dim fromFile As String, fromWorkbook As Workbook

With ActiveWorkbook
With .Worksheets("Sheet1")
Set fileCells = .Range(.Range("A2"), .Cells(.Rows.Count, "A").End(xlUp))
End With
Set destCells = .Worksheets("Sheet2").Range("A1:Y300")
End With

Application.ScreenUpdating = False

r = 0
For Each fileCell In fileCells
fromFile = fileCell.Value
If Right(fromFile, 1) <> "\" Then fromFile = fromFile & "\"
fromFile = fromFile & fileCell.Offset(, 1).Value
Set fromWorkbook = Workbooks.Open(fromFile)
destCells.Offset(r).Value = fromWorkbook.Worksheets("2022 - BMW").Range("A1:Y300").Value
fromWorkbook.Close SaveChanges:=False
r = r + 300
DoEvents
Next

Application.ScreenUpdating = True

MsgBox "Finished"

End Sub
 
Upvote 0
I don't work in VBA for this type of scenario. You will need to wait for a VBA proficient individual. Just so you know, PQ will be faster than any VBA code you finally end up with.
 
Upvote 0
Thank you kindly alansidman. I look forward to the vba assistance per my request to compare.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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