Loop code to copy data from multiple workbooks into different sheet

Lizard07

Board Regular
Joined
Jul 20, 2011
Messages
103
Hello - I currently have a loop code that is copying data from multiple workbooks into a master workbook. The problem is I need it to copy the data from each workbook and paste it into its corresponding sheet.

For instance, the data from the workbook "Aberdeen" I would like to go in the data sheet labeled "Aberdeen", and so on. The sheets are in the same order as the workbooks are opened so I could just have it copy into the next sheet after "Aberdeen" and so on. Names would not have to be a factor.

Please see code below

Sub Create_Month_Summary()
Dim folderPath As String
Dim fileName As String
Dim thisWorkbook As Workbook
Dim dayNumber As Integer
Dim workbookDate As Date
Dim rowOffset As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False

'Folder containing daily dated workbooks for a month - CHANGE AS REQUIRED

folderPath = "P:\PROJECT\XATA Data Tracking\Activity Reports"

Set thisWorkbook = ActiveWorkbook

If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"

rowOffset = 0
fileName = Dir(folderPath & "*.xlsm")
Do While fileName <> ""

'Copy data from dated workbook to associated row in summary sheet

Workbooks.Open folderPath & fileName

With thisWorkbook.Sheets("Aberdeen").Range("A1:C100")
.Offset(rowOffset, 0).Value = Sheets("Trailer Summary").Range("A8:C100").Value
End With
ActiveWorkbook.Close savechanges:=False

'Get next file name

fileName = Dir
Loop

MsgBox "Finished"

Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub

Please let me know if this is not clear, thanks!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,224,587
Messages
6,179,734
Members
452,939
Latest member
WCrawford

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