Hi,
I have the following code that I used for a prior project using Excel 2003 and its related libraries. The code was used to pull data from a folder full of excel files that all had the same single page layout but different data and combine the data in a single target-workbook.
I am now trying to use the same code with Excel 2010 to go through a folder of new excel files that have a different layout than in the prior project. With the new files (see attached example) I want to copy the source data from the "Enter Data Here" sheet and add it to the destination workbook that is running the macro. (Please note that the "Enter Data Here" is not always consistently the same sheet number from one file to the next, so we would need to reference the sheet name and not the number.)
- I was planning to have the destination workbook receive the combined data on the sheet called "Pasteit".
When I run the code in Excel 2010, it opens the first file and then hangs on line 28. I feel like I'm missing something obvious.... your advice would be greatly appreciated!
Sub CombineSourceFiles()
Dim DstRng As Range
Dim DstLastRow As Long
Dim FileFilter As String
Dim SrcLastRow As Long
Dim SrcRng As Range
Dim Sourcefile As String
Dim ofs As New FileSystemObject
Dim strFolder As String
Dim oFile As Object
Dim oFolder As Object
Dim wrkInput As Workbook
Dim wrkOutput As Workbook
Set wrkOutput = ThisWorkbook
'Set input folder path here, change to D
strFolder = "D:\My Documents\B\PROJECTS\Printer REFRESH\CutSheets\Process"
Set oFolder = ofs.GetFolder(strFolder)
For Each oFile In oFolder.Files
Set wrkInput = Application.Workbooks.Open(strFolder & "\" & oFile.Name)
With wrkOutput.Worksheets("Pasteit")
DstLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set DstRng = .Range(.Cells(3, "A"), .Cells(DstLastRow, "K"))
End With
Set SrcWkb = wrkInput
With SrcWkb.Worksheets("Enter Data Here")
SrcLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set SrcRng = .Range(.Cells(3, "A"), .Cells(SrcLastRow, "K"))
End With
SrcRng.Copy Destination:=DstRng.Cells(DstLastRow, "A")
Application.CutCopyMode = False
SrcWkb.Close
Next
End Sub
Bookmarks