0

I am trying to copy multiple Excel workbooks into a single workbook with each of the copied workbooks to have their own Tab.

I do not know how to use VB, so I have tried many VB scripts I found using Google, but for one reason or another (error 9, error 91, nothing happens, all workbooks gets copied into one tab) I have not managed to get it done.

3
  • 1
    VBA is used in excel. What scripts have you tried? What happened in each one? What didn't happen? How do you handle a workbook with multiple sheets? Feb 9, 2016 at 15:55
  • You have accidentally created this question using an unregistered account and then created a new account and posted a comment as an answer. In order to regain control of your question please go to the contact form (superuser.com/contact) and select "I need to merge accounts" and enter your old account (superuser.com/users/556021/bryan-gervais) and your new account (superuser.com/users/556029/bryan-g) in the two boxes. This will allow you to edit, comment on and accept answers to your question.
    – Mokubai
    Feb 9, 2016 at 17:59
  • Nothing seems to work fo rme when I try to merge my quest and my registered account, and when I try to give Votes whilke I am logged in, it asks me to log in again. Anyhow my Excel Problem is resolved with this code: Sub GetSheets() Path = "<INSERT PATH TO DIRECTORY HERE>" Filename = Dir(Path & ".xls") Do While Filename <> "" Workbooks.Open Filename:=Path & Filename, ReadOnly:=True For Each Sheet In ActiveWorkbook.Sheets Sheet.Copy After:=ThisWorkbook.Sheets(1) Next Sheet Workbooks(Filename).Close Filename = Dir() Loop End Sub
    – Bryan G
    Feb 10, 2016 at 13:26

2 Answers 2

3

If you don't have a lot of workbooks, you can do it manually by following these instructions. Relevant excerpts:

  • Right-click on the tab you want to move and choose move or copy
  • Select the target book in the pull list
  • Select the tab location where you would like it in the target book
  • Click ok

If you have a lot of workbooks, you can automated it by following these instructions. Relevant excerpts:

  • Put all the workbooks in the same directory and note the directory path
  • Open the target workbook
  • Click Developer -> Visual Basic
  • In new window, click Insert -> Module
  • Paste the following code:

    Sub GetSheets()
    Path = "<INSERT PATH TO DIRECTORY HERE>"
    Filename = Dir(Path & "*.xls*")
      Do While Filename <> ""
      Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
        For Each Sheet In ActiveWorkbook.Sheets
        Sheet.Copy After:=ThisWorkbook.Sheets(1)
      Next Sheet
        Workbooks(Filename).Close
        Filename = Dir()
      Loop
    End Sub
    
  • Replace the Path line with the full path to the directory with the workbooks

  • Click the green arrow buttonto run the code and merge the workbooks.
7
  • I tried this and nothing happens. I get the Makros window, and choose Run, and then nothing further happens. No Errors either.
    – Bryan G
    Feb 10, 2016 at 10:09
  • What version of Excel are you using? Feb 10, 2016 at 13:13
  • And what is the full path to the directory containing the workbooks you wish to merge? Feb 10, 2016 at 13:14
  • Thanks your script worked! Now I just Need to figure out how to make it automatically use a particular field with a 4 Digit number in each of the Excel Sheets that are being copied over, and with that 4 Digit number, rename each worksheet to each number respectively. Is there a way to edit the script to achieve this? BTW, I am not able to give you a Vote, it asks me to log in each time although I am logged in. maybe someone else can do it for me?
    – Bryan G
    Feb 10, 2016 at 13:31
  • You can follow these instructions to dynamically rename the worksheets. Probably easiest to add it as a second, separate script, though you can try integrating if you want: support.microsoft.com/en-us/kb/2790229 Feb 10, 2016 at 13:35
1

This works -

    Sub CopyBooks()
    Application.ScreenUpdating = False
    Application.Calculation = xlManual

    Dim destinationWorkbook As Workbook
    Set destinationWorkbook = ThisWorkbook
    Dim sourceWorkbook As Workbook
    Dim sourceWorksheet As Worksheet
    Const path As String = "C:\your\path\"
    Dim file As Variant

    Dim currentSheets As Long
    currentSheets = destinationWorkbook.Sheets.Count

    file = Dir(path & "*.xl*")

    While file <> ""
        Set sourceWorkbook = Workbooks.Open(path & file)
            For Each sourceWorksheet In sourceWorkbook.Worksheets
                sourceWorksheet.Copy after:=destinationWorkbook.Worksheets(currentSheets)
                currentSheets = currentSheets + 1
            Next
            sourceWorkbook.Close savechanges:=False
            file = Dir
    Wend

    Application.Calculation = xlAutomatic
    Application.ScreenUpdating = True
    End Sub
2
  • I tried this and nothing happens. I get the Makros window, and choose Run, and then nothing further happens. No Errors either.
    – Bryan G
    Feb 10, 2016 at 10:09
  • 1
    Make sure your path points to where your workbooks are Feb 10, 2016 at 11:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .