Excel 2016 VBA to consolidate data from several workbooks into one book

Kthom019

New Member
Joined
May 16, 2017
Messages
46
Can someone help with a macro to consolidate data from several workbooks into one workbook. I recorded a macro by copying and pasting the data from their workbooks into the required worksheet in the new workbook. But my mind is saying that there is another way to do this; is there?
 
Message received: Run-time error 9, 'Subscript out of range'
I entered the below argument. I am not sure what is wrong. May you kind to help me understand the script and why the error, thanks.

Sub Consolidate_Files()
Dim Ary As Variant
Dim i As Long
Dim Wbk As Workbook

Set Wbk = Workbooks("Forecast test1.xlsm")
Ary = Array("Jas", "Bas", "Tas", "Bar")

For i = 0 To UBound(Ary)
Workbooks(Ary(i) & ".xlsm").Sheets("Jas").Range("D3:O369").Copy Wbk.Sheets(Ary(i)).Range("D3")
Next i
End Sub







Is this what you need?
Code:
Sub Kthom019()
   Dim Ary As Variant
   Dim i As Long
   Dim Wbk As Workbook
   
   Set Wbk = Workbooks("Forecast.xlsm")
   Ary = Array("JAS", "BAS", "TAS", "BAR")
   
   For i = 0 To UBound(Ary)
      Workbooks(Ary(i) & ".xlsm").Sheets("Fcast").Range("D3:O369").Copy Wbk.Sheets(Ary(i)).Range("D3")
   Next i
End Sub
Change file extension if needed.
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
What is the name of the sheet that you are trying to copy from?
 
Upvote 0
This was what I did.

'Workbooks("Userform 25.xlsm").Sheets("Jas").Range("A1:A30").Copy Workbooks("Copy Shapes.xlsm").Sheets("Jas-1").Range("D6")'

Remember, I am attempting to Copy data from workbook 'Jas' range D6:0360 into Workbook "Forecast - worksheet-'Jas-1', range D6:O360"
 
Upvote 0
I am copying data from workbooks Jas, Bas, Tas, & Bar, worksheet name "FCAST" into workbook Forecast test1, with worksheets have the same names, Jas, Bas, Tas, & Bar.
example: data from the 'Jas' workbook sheet' FCAST', is being copied into workbook 'Forecast test1' worksheet 'JAS'
data from the 'Bas' workbook sheet' FCAST', is being copied into workbook 'Forecast test1' worksheet 'BAS'
data from the 'Tas' workbook sheet' FCAST', is being copied into workbook 'Forecast test1' worksheet 'TAS'
data from the 'Bar' workbook sheet' FCAST', is being copied into workbook 'Forecast test1' worksheet 'Bar'
After all sheets are copied over, I want the 'Total" sheet, which is also in workbook 'Forecast test1', to consolidate the records from all 4 files into one (summing all 4 sheets)
The ranges in all sheets are the same. D6:O360.

Sub Consolidate_Files()
Dim Ary As Variant
Dim i As Long
Dim Wbk As Workbook

Set Wbk = Workbooks("Forecast test1.xlsm")
Ary = Array("Jas", "Bas", "Tas", "Bar")

For i = 0 To UBound(Ary)
Workbooks(Ary(i) & ".xlsm").Sheets("Jas").Range("D3:O369").Copy Wbk.Sheets(Ary(i)).Range("D3")
Next i
End Sub
 
Upvote 0
I had changed the range also

'Workbooks("Userform 25.xlsm").Sheets("Jas").Range("D6:O360").Copy Workbooks("Copy Shapes.xlsm").Sheets("Jas-1").Range("D6")'


This was what I did.

'Workbooks("Userform 25.xlsm").Sheets("Jas").Range("A1:A30").Copy Workbooks("Copy Shapes.xlsm").Sheets("Jas-1").Range("D6")'

Remember, I am attempting to Copy data from workbook 'Jas' range D6:0360 into Workbook "Forecast - worksheet-'Jas-1', range D6:O360"
 
Upvote 0
Well I'm not sure what your saying.

Do you really have a Workbook named:
Userform 25.xlsm

I think not.

You need to change this to what your workbook is named.

If your workbook is named "Jas" then change my script.
 
Upvote 0
Are the sheets you are copying too called Jas, Bas etc, or Jas-1, Bas-1 etc?
the names of these sheets needs to be exact.
 
Upvote 0
If the sheets are Jas-1 etc, try
Code:
Sub Kthom019()
   Dim Ary As Variant
   Dim i As Long
   Dim wbk As Workbook
   
   Set wbk = Workbooks("Forecast test1.xlsm")
   Ary = Array("JAS", "BAS", "TAS", "BAR")
   
   For i = 0 To UBound(Ary)
      Workbooks(Ary(i) & ".xlsm").Sheets("Fcast").Range("D3:O369").Copy wbk.Sheets(Ary(i) & "-1").Range("D3")
   Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,943
Messages
6,127,814
Members
449,409
Latest member
katiecolorado

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