You are here:

Excel/Macro to copy data from different workbooks to one workbook


I have folders created basis each day in a month in this format "YYYYMMDD". Each folder has four worbooks called as 1,2,3,4. A VBA code is required
to copy data from all four wokbooks(1,2,3,4) and should copy paste data(used range) in a new workbook in four different worksheets(sheet1, Sheet2, sheet3, sheet4).
Appreciate your help in this regard .



in this macro, I assume you have four workbooks

each with a single worksheet in them.  I copy the worksheet to a single new workbook.

You get a dialog to select whichever directory you want to process.

Sub CopyFiles()
Dim fldr As FileDialog
Dim sItem As String
Dim bk As Workbook, bkMaster As Workbook

Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
   .Title = "Select a Folder"
   .AllowMultiSelect = False
   .InitialFileName = strPath
   sItem = .SelectedItems(1)
End With
Workbooks.Add Template:=xlWBATWorksheet
Set bkMaster = ActiveWorkbook
If Right(sItem, 1) <> "\" Then sItem = sItem & "\"
v = Array("1", "2", "3", "4")
For i = LBound(v) To UBound(v)
 Workbooks.Open sItem & v(i) & ".xlsx"
 Set bk = ActiveWorkbook
 bk.Worksheets(1).Copy After:=bkMaster.Worksheets(bkMaster.Worksheets.Count)
 bk.Close SaveChanges:=False
Application.DisplayAlerts = False
Application.DisplayAlerts = True
End Sub

that should get you started.  

It was tested with the stated assumptions and worked fine for me.

Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 All rights reserved.