consolidating multiple excel worksheets across multiple workbooks

wao5012

New Member
Joined
May 7, 2018
Messages
32
Hello All,

I am trying to use the below script to consolidate multiple excel worksheets across multiple workbooks.

I have 10 excel files that contain roughly 20 worksheets (tabs). I am trying to put the data on worksheet 1 (tab 1 called summary) and worksheet 2 (tab 2 called All Breaks) in a new workbook with two tabs one with the consolidated Summary tabs and one with the the consolidated All Breaks tab.

I am trying to use the below script to achieve this. However it keeps failing and returning an error "Object Required" when it gets to the highlighted text (in red) below. I was wondering if anyone knew how to achieve the above?

Code:
Sub Copydata()


Dim FolderPath As String, Filepath As String, Filename As String
FolderPath = "C:\Users\woswald\Documents\Castlelake"
Filepath = FolderPath & "*.xls*"
Filename = Dir(Filepath)
Dim erow As Long, lastrow As Long, lastcolumn As Long


Do While Filename <> ""
Dim wb As Workbook
Set wb = Workbooks.Open(FolderPath & Filename)
For counter = 1 To 2
    'Sheets("Sheet1").Select
wb.Worksheets(counter).Activate
[COLOR=#ff0000]lastrow = ActiveSheet.Cells(Row.Count, "A").End(xlUp).Row[/COLOR]
lastcolumn = ActiveSheet.Cells(A, Column.Count).End(xlToLeft).Column
Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy


    'Sheets("Sheet1").Select
Workbooks("Investran_Colsoildated.xlsm").Worksheets(counter).Activate
erow = ActiveSheet.Cells(Rows, Count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 1).Select
ActivateSheet.Paste
Next
wb.Close savechanges:=False


Loop


erow = ActivateSheet.Cells(Row, Count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 1).Select
 
Last edited by a moderator:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi & welcome to the board.
It should be
Code:
Row[COLOR=#ff0000]s[/COLOR].Count
 
Upvote 0
Thank You Fluff,

I am now getting a fail at the next line below for "Type Mismatch", I am wondering what this error means and how I can fix it? After looking at Fluffs code I changed the line below.

Before
lastcolumn = ActiveSheet.Cells(A, Column.Count).End(xlToLeft).Column

After
lastcolumn = ActiveSheet.Cells("A", Columns.Count).End(xlToLeft).Column
 
Upvote 0
You'll also need to change the erow= line as well
 
Upvote 0
Okay I made those updates. When I run the script i get a msgbox that pops up from excel "There is a large amount of information on the Clipboard. Do you want to be able to paste this information into another program later (do i hit yes or no) and within the script it fails because

"Method 'Close of object'_Workbook' Failed" on the below line

wb.Close savechanges:=False
 
Upvote 0
Try
Code:
ActivateSheet.Paste
Next
Application.CutCopyMode = False
wb.Close False
FileName = Dir()

Loop
One word of warning, I would advise against using VBA keywords for variables & sub Names, as it can cause problems.
 
Upvote 0
Ok thank you for the advise. I am receiving a error "subscript out of range" on the below line. There is a file saved out to the same location as on the file.

Workbooks("Investran-Consolidated.xlsm").Worksheets(counter).Activate
 
Upvote 0
Try
Code:
Workbooks("Investran_Colsoildated.xlsm").Activate
Workbooks("Investran_Colsoildated.xlsm").Worksheets(counter).Activate
 
Upvote 0
I am still getting the same error “Subscript out of range” but now the error is on the first line that we added (highlighted in Red). Would it be easier to just have the code create a new workbook to save to instead of having it find the file and then saving to it? If so, how what that is done? Sorry I am new to VBA

Workbooks("Investran_Colsoildated.xlsm").Activate
Workbooks("Investran_Colsoildated.xlsm").Worksheets(counter).Activate
 
Upvote 0
Do you have a workbook called Investran_Colsoildated.xlsm & if so, is it open?
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,588
Members
449,089
Latest member
Motoracer88

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