+ Reply to Thread
Results 1 to 3 of 3

Copy data from multiple workbooks to one master workbook

Hybrid View

  1. #1
    Registered User
    Join Date
    01-09-2014
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    2

    Copy data from multiple workbooks to one master workbook

    Hi all,

    Probably the 6324th time this question has been asked BUT:

    I have four excel workbooks in a folder. 2 are .xls and other 2 are .xlsm. None of these workbooks have an equal
    amount of sheets. My question is if anyone can help me start up in VBA to open these files, copy the sheets to one
    masterfile and close the four workbooks.

    Any help for a start-up would be appreciated!

    Thank you.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,862

    Re: Copy data from multiple workbooks to one master workbook

    6324 ... well, you probably underestimated :-) (by the way - have you used search?)

    Try (it is based on a code I am using elsewhere, so some of variables have strange names :-P) in your masterfile:
    Sub copy_all()
    Dim my_wbk As Workbook, src_wbk As Workbook, src_wks As Worksheet
    Dim ob As Object, pliki As Object, plik As Object
    Dim folder As Object, mypath As String
    Set my_wbk = ThisWorkbook
    mypath = "C:\Users\here_your_real_path\"
    Set ob = CreateObject("Scripting.FilesystemObject")
    Set folder = ob.GetFolder(mypath)
    Set pliki = ob.GetFolder(mypath).Files
    
    With Application
     .ScreenUpdating = False
     .EnableEvents = False
    End With
    
    For Each plik In pliki
      Debug.Print plik.Name
      
      If Right(plik.Name, 3) = "xls" Or Right(plik.Name, 4) = "xlsm" Then
         Set src_wbk = Workbooks.Open(folder & "\" & plik.Name)
         For Each src_wks In src_wbk.Worksheets
    Debug.Print "   -> " & src_wks.Name
           src_wks.Copy After:=my_wbk.Sheets(my_wbk.Sheets.Count)
         Next src_wks
         src_wbk.Close False
       End If
    Next plik
    
    With Application
      .ScreenUpdating = True
      .EnableEvents = True
    End With
    
    Set my_wbk = Nothing
    Set ob = Nothing
    Set folder = Nothing
    Set pliki = Nothing
    End Sub
    but note that there will be problems if sheets in different source files have the same names - of course you can rename them first - just be aware of it.

  3. #3
    Registered User
    Join Date
    01-09-2014
    Location
    Amsterdam, Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Copy data from multiple workbooks to one master workbook

    Hi Kaper,

    Thanks for your effort!

    Code seems to work fine but excel stucks and does not respond anymore.

    Think I know what bugs here. The .xls files contain macros. When I open a .xls file it asks me to "Enable Content". Do you have a clue how to avoid this?

    Thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Copy Data from multiple workbooks to a master Workbook
    By zeeez in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-04-2012, 01:45 PM
  2. Copy data from multiple workbooks into consolidated/master workbook
    By mobro1234 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-02-2012, 02:42 PM
  3. Copy Data from Multiple Workbooks into Master Workbook Daily
    By gilbare in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-11-2012, 05:45 PM
  4. Replies: 5
    Last Post: 03-05-2012, 09:31 AM
  5. Replies: 24
    Last Post: 02-17-2012, 11:30 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1