combining multiple worksheets from many different files into single worksheet

jduivest

New Member
Joined
Feb 28, 2011
Messages
3
Hi there, I don't have much experience with macros, but I've recently started using them to process a large amount of data. I'm on (what I hope is) the final step, but I'm stuck, so I need a bit of help. I've done a search and found several similar questions, but unfortunately none exactly like this.

Essentially, I have several hundred spreadsheets in a folder. Each of these has two worksheets (lets call them Sheet X and Sheet Y). What I'd like to do is set up a macro that will automatically import this information into a master file. Each file is for a different day, and I'd like to combine everything so I can look at changes over time.

The details:

Each Sheet X has data entries on several different columns, from Row 1 to Row N. I'd like the master file will have a Sheet X, where all Sheet X (one from each spreadsheet file) are added sequentially. So Day 1 Sheet X is copied in from Rows 1 to N, Day 2 Sheet X is copied in from Rows N+1 to 2N, etc.

The same process happens for Sheet Y.

All files are formatted identically, and are in the same folder. I'm using Excel 2007, if that matters.

Note, Sheet X and Sheet Y are not linked to each other, so it would be fine for them to be in two different files. The important thing is that all Sheet X are combined into a single master Sheet X, and all Sheet Y are combined into a single master Sheet Y.

Thanks so much!!!
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Here’s how I would accomplish this<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
I didn’t test and you may have to tweak it but it should get you going<o:p></o:p>
<o:p> </o:p>
-use a DOS command to get a list of the workbooks in your folder<o:p></o:p>
Example: DOS Command dir /a "C:\Program Files\xorb\" > C:\dirlist.txt<o:p></o:p>
(if you need help on above, google it)<o:p></o:p>
<o:p> </o:p>
Create a Masterlist Workbook<o:p></o:p>
Create 3 Sheets<o:p></o:p>
*Sheet X<o:p></o:p>
*Sheet Y<o:p></o:p>
*Workbooks (let’s say there are 100 workbooks in your folder)<o:p></o:p>
-in above Workbooks worksheet, paste a list of the complete file paths of your workbooks in column A (e.g. c:\temp\mydoc.xls)<o:p></o:p>
<o:p> </o:p>
Dim MasterListWorkbook as Workbook<o:p></o:p>
Set MasterListWorkbook as ActiveWorkbook<o:p></o:p>
<o:p> </o:p>
Dim WorkbookFilePath as String<o:p></o:p>
<o:p> </o:p>
Dim i as Integer ‘for workbook<o:p></o:p>
Dim j as Long ‘row on masterlist Sheet X<o:p></o:p>
Dim k as Long ‘row on masterlist Sheet Y<o:p></o:p>
Dim lastrow as Long ‘last row on the workbook we are obtaining data from<o:p></o:p>
<o:p> </o:p>
Dim OpenWorkbook as Workbook<o:p></o:p>
<o:p> </o:p>
j = 1<o:p></o:p>
k = 1<o:p></o:p>
<o:p> </o:p>
For i = 1 to 100<o:p></o:p>
<o:p> </o:p>
MasterListWorkbook.Activate<o:p></o:p>
Worksheets(“Workbooks”).Activate<o:p></o:p>
WorkbookFilePath = Cells(i,1)<o:p></o:p>
<o:p></o:p>
Workbooks.Open (WorkbookFilePath)<o:p></o:p>
Set OpenWorkbook = ActiveWorkbook<o:p></o:p>
<o:p></o:p>
Worksheets(“Sheet X”).Activate<o:p></o:p>
LastRow = Cells.SpecialCells(xlcelltypelastcell).Row<o:p></o:p>
Rows(“1:” & LastRow).Copy<o:p></o:p>
MasterListWorkbook.Activate<o:p></o:p>
Worksheets(“Sheet X”).Activate<o:p></o:p>
Range(“A” & j).Paste<o:p></o:p>
j = j + LastRow<o:p></o:p>
<o:p> </o:p>
OpenWorkbook.Activate<o:p></o:p>
Worksheets(“Sheet Y”).Activate<o:p></o:p>
LastRow = Cells.SpecialCells(xlcelltypelastcell).Row<o:p></o:p>
Rows(“1:” & LastRow).Copy<o:p></o:p>
MasterListWorkbook.Activate<o:p></o:p>
Worksheets(“Sheet Y”).Activate<o:p></o:p>
Range(“A” & k).Paste<o:p></o:p>
k = k + LastRow<o:p></o:p>
<o:p> </o:p>
OpenWorkbook.Close SaveChanges:=False<o:p></o:p>
<o:p> </o:p>
Next i<o:p></o:p>
 
Upvote 0
Hi, thanks for the quick reply.

I've pasted it, and given it a try.

MasterListWorkbook.Activate didn't seem to work, so I replaced it with Windows("MasterListWorkbook.xlsx").Activate

AFAIK, this should also be ok, as long as I have the file open at the time?

However, I'm stuck at the following:
Range("A" & j).Paste

I'm being told this property/method isn't supported. Any suggestions?

Thanks
 
Upvote 0
Yahoo, got it to work. Thanks!

Just in case anyone else has the same issue, here's a few changes I made from the original code, to fix the bugs that came up.


Add
Application.CutCopyMode = False
before k = k + lastrow.


This prevents a dialog box from popping up every time the macro tries to close the files you're copying from.

-----

I couldn't get the Rows command to work, so I used the following:
Range("A1").Offset(j, 0).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=False

-----
Workbooks.Open (WorkbookFilePath)

was replaced by

Windows("MasterListWorkbook.xlsx").Activate


Other than that, it worked great! Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,561
Messages
6,179,521
Members
452,923
Latest member
JackiG

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