Consolidating data from multiple workbooks into a master

piernt

New Member
Joined
Mar 5, 2014
Messages
2
Hello. I have only been working with macros/VBA for a week, so I know there are simple issues with my code, but I've turned it into spaghetti code trying to debug.
I am trying to load data from multiple workbooks into a master and really need help please!!

Dir for source files: C:\Test Dir\
Dir for Master: C:\Test Dir\Master\

Source filenames differ, but all end in "*FORMATTED.xlsx."
Master filename: "Payroll Master.xlsx"
Source worksheet name = "Loaded Data"
All SOURCE data is in rows 2-106 and columns A through J.

The Master file has the same column headers as the source files and I am loading all data into the "Summary" worksheet.

My latest error is a runtime 91, where MyTemplate is defined. Please help!

This is my current code:

Sub combine_data()
'
Dim MyPath As String
Dim SumPath As String
Dim MyName As String
Dim SumName As String
Dim MyTemplate As Workbook
Dim SumTemplate As Workbook
MyPath = "C:\Test Dir\"
SumPath = "C:\Test Dir\Master\"
MyTemplate = "*.xlsx" 'Set the template.
SumTemplate = "Payroll MASTER.xlsx"
MyName = Dir(MyPath & MyTemplate) 'Retrieve the first file
SumName = Dir(SumPath & SumTemplate)

Do While MyName <> ""
Workbooks.Open (MyName)
Sheets("Loaded Data").Range("A2:J106").Copy
'Selection.Copy
Workbooks.Open (SumName)
Range("A65536").End(xlUp).Offset(1, 0).Activate
Selection.PasteSpecial Paste:=xlPasteValues
Workbooks(MyName).Close SaveChanges:=False 'close
Workbooks(SumName).Close SaveChanges:=True
MyName = Dir 'Get next file
Loop
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi Piernt,

it does look like you're on the right track. I would open the summary file only once, then open the source files one by one. I made some small modifications to your code, wasn't able to test it, but hope that this gets you moving again:

Cheers,

Koen


Code:
Sub combine_data()
'
Dim MyPath As String
Dim SumPath As String
Dim MyName As String
Dim SumName As String
Dim MyTemplate As Workbook
Dim SumTemplate As Workbook

MyPath = "C:\Test Dir\"
SumPath = "C:\Test Dir\Master\"

MyTemplate = "*.xlsx" 'Set the template.
SumTemplate = "Payroll MASTER.xlsx"

MyName = Dir(MyPath & MyTemplate) 'Retrieve the first file
Set SumWb = Workbooks.Open(SumPath & SumTemplate)

Do While MyName <> ""
    Set ImpWb = Workbooks.Open(MyName, False, True)
    
    ImpWb.Worksheets("Loaded Data").Range("A2:J106").Copy
    SumWb.Worksheets("ResultSheet").Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    
    Workbooks(MyName).Close SaveChanges:=False 'close
    
    MyName = Dir 'Get next file
    Set ImpWb = Nothing
Loop

SumWb.Close SaveChanges:=True
    
Set SumWb = Nothing
Set ImpWb = Nothing

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,073
Members
449,205
Latest member
Healthydogs

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