Copy from multiple worksheets and multiple source files in a folder and paste into multiple sheets in target workbook

Hs72711

New Member
Joined
May 26, 2023
Messages
1
Hi I have the code below however I need it to pick up a range of tabs in both the source files and the target files. The worksheets range from WK1 to Wk 53.

Option Explicit


Const FOLDER_PATH = "C:\temp\Users\User1\My Documents\Times\" 'REMEMBER END BACKSLASH


Sub ImportWorksheets()
'=============================================
'Process all Excel files in specified folder
'=============================================
Dim sFile As String 'file to process
Dim wsTarget As Worksheet
Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim rowTarget As Long 'output row

rowTarget = 2

'check the folder exists
If Not FileFolderExists(FOLDER_PATH) Then
MsgBox "Specified folder does not exist, exiting!"
Exit Sub
End If

'reset application settings in event of error
On Error GoTo errHandler
Application.ScreenUpdating = False

'set up the target worksheet
Set wsTarget = Sheets("Sheet2")

'loop through the Excel files in the folder
sFile = Dir(FOLDER_PATH & "*.xls*")
Do Until sFile = ""

'open the source file and set the source worksheet - ASSUMED WORKSHEET(1)
Set wbSource = Workbooks.Open(FOLDER_PATH & sFile)
Set wsSource = wbSource.Worksheets(1) 'EDIT IF NECESSARY

'import the data
With wsTarget
.Range("A" & rowTarget).Value = wsSource.Range("C2").Value
.Range("B" & rowTarget).Value = wsSource.Range("D2").Value
.Range("C" & rowTarget).Value = wsSource.Range("E2").Value
.Range("D" & rowTarget).Value = wsSource.Range("F2").Value
.Range("E" & rowTarget).Value = wsSource.Range("B7").Value
.Range("F" & rowTarget).Value = wsSource.Range("H5").Value
.Range("G" & rowTarget).Value = wsSource.Range("L6").Value
.Range("H" & rowTarget).Value = wsSource.Range("M7").Value
.Range("I" & rowTarget).Value = wsSource.Range("N8").Value
.Range("J" & rowTarget).Value = wsSource.Range("N9").Value
.Range("K" & rowTarget).Value = wsSource.Range("N10").Value
.Range("L" & rowTarget).Value = wsSource.Range("N11").Value
.Range("M" & rowTarget).Value = wsSource.Range("N12").Value

'optional source filename in the last column
.Range("N" & rowTarget).Value = sFile
End With

'close the source workbook, increment the output row and get the next file
wbSource.Close SaveChanges:=False
rowTarget = rowTarget + 1
sFile = Dir()
Loop

errHandler:
On Error Resume Next
Application.ScreenUpdating = True

'tidy up
Set wsSource = Nothing
Set wbSource = Nothing
Set wsTarget = Nothing
End Sub




Private Function FileFolderExists(strPath As String) As Boolean
If Not Dir(strPath, vbDirectory) = vbNullString Then FileFolderExists = True
End Function
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,224,813
Messages
6,181,114
Members
453,021
Latest member
Justyna P

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