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

Segregating Numbers According to Their Sign

Remember your number line from your early years in school? Some numbers can be below zero (negative numbers) and others ...

Discover More

Understanding Monospace Fonts

Monospace fonts allow you to easily achieve a specific "look" with your text or to line up information in a certain way. ...

Discover More

Formatting Footnote Reference Marks

The reference marks that appear for footnotes in a document are normally just superscripted digits. If you want to change ...

Discover More

Best-Selling VBA Tutorial for Beginners Take your Excel knowledge to the next level. With a little background in VBA programming, you can go well beyond basic spreadsheets and functions. Use macros to reduce errors, save time, and integrate with other Microsoft applications. Fully updated for the latest version of Office 365. Check out Microsoft 365 Excel VBA Programming For Dummies today!

More ExcelTips (ribbon)

Strange Message about Others Making Changes in a Workbook

Have you ever tried to save a workbook, only to be notified that someone else has made changes in it? What if you are the ...

Discover More

Opening Multiple Workbooks at Once

Need to open a bunch of workbooks from within Excel? It's easy to do when you construct a selection set in the Open ...

Discover More

Printing Workbook Properties

Want to create a printed record of the properties associated with a workbook? There is no easy way to do it in Excel. ...

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 two more than 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.