Copying a Single Worksheet from Many Workbooks

Written by Allen Wyatt (last updated March 17, 2025)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365


2

Victor has a ton of workbooks containing monthly financial information for the past ten years. (One workbook for each month in that decade.) He needs to create a workbook that contains the third worksheet from each of these, so he'll end up with a workbook that contains 120 worksheets. Victor knows he can open each workbook and copy the desired worksheets, one at a time, but that is quite tedious. He wonders if there is a way to easily copy the worksheets to a single, new workbook.

This type of repetitive, tedious task is best addressed by a macro. This example will create a new workbook and then look through all the Excel files in a folder (specified in the sPath variable) to get the third worksheet.

Sub CombineThirdWorksheets()
    Dim sPath As String
    Dim Filename As String
    Dim wb As Workbook
    Dim DestWb As Workbook

    ' Set the folder path (make sure path ends with a backslash)
    sPath = "C:\Path\To\Your\Directory\"

    ' Create a new workbook to store the third worksheets
    Set DestWb = Workbooks.Add

    ' Get the first Excel file in the directory
    Filename = Dir(sPath & "*.xls*")

    ' Loop through all Excel files in the directory
    Do While Filename <> ""
        ' Open the current Excel file
        Set wb = Workbooks.Open(sPath & Filename)

        ' Check if there are at least 3 worksheets
        If wb.WorkSheets.Count >= 3 Then
            wb.WorkSheets(3).Copy After:=DestWb.Sheets(DestWb.Sheets.Count)
        End If

        ' Close the current Excel file without saving changes
        wb.Close SaveChanges:=False

        ' Get the next Excel file in the directory
        Filename = Dir
    Loop

    ' Save the destination workbook
    DestWb.SaveAs Filename:=sPath & _
      "CombinedThirdWorksheets.xlsx", _
      FileFormat:=xlOpenXMLWorkbook
    ' Close the destination workbook
    DestWb.Close

    MsgBox "All third worksheets have been combined."
End Sub

To use the macro, make sure all the workbooks you want to process are in a single folder. Then, change the sPath variable within the macro so that it is the full path to the folder. When you run the macro, it will create a workbook, copy all the "third worksheets" into the workbook, and then save the workbook using the name CombinedThirdWorksheets.xlsx.

You could also take a different approach in your macro, relying upon a scripting approach, as shown here:

Sub MergeSheetThree()
    Dim wbSource As Workbook
    Dim wbDest As Workbook
    Dim wsSource As Worksheet
    Dim fso As Object
    Dim folder As Object
    Dim file As Object
    Dim sPath As String
    Dim fileName As String

    ' Set the folder path (make sure path does NOT end with a backslash)
    sPath = "C:\Your\Folder\Path\Here"

    Set wbDest = Workbooks.Add
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(sPath)

    For Each file In folder.Files
        fileName = file.Name

        If Right(fileName, 5) = ".xlsx" Then
           Set wbSource = Workbooks.Open(sPath & "\" & fileName)
           If wbSource.WorkSheets.Count >= 3 Then
               Set wsSource = wbSource.WorkSheets(3)
               wsSource.Copy After:=wbDest.Sheets(wbDest.Sheets.Count)
           End If
           wbSource.Close False
        End If
    Next

    ' Save the destination workbook
    wbDest.SaveAs Filename:=sPath & "\" & _
      "CombinedThirdWorksheets.xlsx", _
      FileFormat:=xlOpenXMLWorkbook
    ' Close the destination workbook
    wbDest.Close

    Set wbSource = Nothing
    Set wbDest = Nothing
    Set wsSource = Nothing
    Set fso = Nothing
    MsgBox "Merging completed"
End Sub

As with the previous macro, copy all of your workbooks into a single folder and then set the sPath variable to the folder's path. Unlike the previous macro, though, the path should not end with a backslash. In addition, this second macro will only process workbooks that use the XLSX extension, whereas the first one will process any type of workbook (XLS, XLSX, XLSM, and XLSB).

There is one thing that needs to be stressed with either of the macro approaches discussed in this tip—they explicitly fulfill what Victor expressed by simply copying worksheets from one workbook into another. If the worksheet being copied includes formulas that references other worksheets in the source workbook, then those formulas will not work correctly in the copy of the worksheet placed in the target workbook.

Finally, you should note that both macros copy the third worksheet in the Worksheets collection. This may not give you the exact results you expect. For instance, the default names for worksheets added to a workbook are Sheet1, Sheet2, Sheet3, etc. Over time, the worksheets may be moved around, so that Sheet3 is actually the fifth worksheet listed in the worksheet tabs. If you really want Sheet3 to be copied, thinking it is always considered the third worksheet, then you may not get what you expect—a differently named worksheet could occupy the third index position in the Worksheets collection. In such cases, you may be better served to replace Worksheets(3) in either macro with Worksheets("Name of Worksheet") to get the exact one you want. This assumes, of course, that the worksheet you want copied from each workbook uses the same name. You can find more information about worksheet names and worksheet indexes (in the Worksheets collection) at either of these tips:

https://tips.net/T12414
https://tips.net/T11103

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9774) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Copying a File in VBA

Need to have your macro copy a file from one place to another? It's easy to do using the FileCopy command, described in ...

Discover More

Displaying Actual Font Faces

Want to see font samples as you contemplate which fonts to use in your document? Word provides an easy way to do this.

Discover More

Adding Tags to Text

The Find and Replace capabilities of Word can be used to add HTML tags to your document text. This is easier to do than ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 365 applications with VBA programming. Written in clear terms and understandable language, the book includes systematic tutorials and contains both intermediate and advanced content for experienced VB developers. Designed to be comprehensive, the book addresses not just one Office application, but the entire Office suite. Check out Mastering VBA for Microsoft Office 365 today!

More ExcelTips (ribbon)

Discovering Dependent Workbooks

When you starting linking information from one workbook to another, those workbooks become dependent on each other. ...

Discover More

Worksheet Deletions Don't Reduce Workbook File Size

When you delete items from a workbook, it is natural to think that the workbook's file size should decrease. This may not ...

Discover More

Remembering Workbook Settings from Session to Session

You can spend a lot of time getting your workbook to look "just right." Wouldn't it be great if Excel was able to ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 3?

2025-03-20 13:23:34

J. Woolley

Re. my previous comment below, here is a better version of the replacement statement:
If LCase(Mid(Filename, InStrRev(Filename, "."), 4)) = ".xls" Then
This version catches an old workbook of type XLS, but the previous version did not. Mea culpa.


2025-03-17 12:47:15

J. Woolley

The Tips says, "...this second macro will only process workbooks that use the XLSX extension, whereas the first one will process any type of workbook (XLS, XLSX, XLSM, and XLSB)." That restriction can be eliminated if this statement in the second macro
        If Right(fileName, 5) = ".xlsx" Then
is replaced by this statement
        If LCase(Left(Right(Filename, 5), 4)) = ".xls" Then


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.