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


6

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

Clip Art Sizing Difficulties

A discussion of problems a reader was having resizing clip art in Word.

Discover More

Pulling Cell Names into VBA

Excel allows you to define names that can refer either to ranges of cells or to constant information, such as formulas. ...

Discover More

Using the Object Browser

Efficiently navigating through a document, particularly as it gets longer, can be a perpetual challenge. One tool you can ...

Discover More

Program Successfully in Excel! This guide will provide you with all the information you need to automate any task in Excel and save time and effort. Learn how to extend Excel's functionality with VBA to create solutions not possible with the standard features. Includes latest information for Excel 2024 and Microsoft 365. Check out Mastering Excel VBA Programming today!

More ExcelTips (ribbon)

Sharing Your Workbook

Need to allow others to contribute to your Excel workbook? It's easy to do if you just share it. This tip provides an ...

Discover More

Creating a Workbook Clone

If you are afraid of messing up a workbook, consider doing your work on a clone of the workbook. Excel provides an easy ...

Discover More

Seeing All Open Workbook Names

Ever want to see a list of all the workbooks that are open? If you open more than nine, Excel only displays the first ...

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 seven more than 1?

2025-09-24 12:04:56

J. Woolley

@Dan
Is it "compilation errors" or a run-time error?
Do you have a folder named C:\Path\To\Your\Directory\?
Does every workbook in that folder have a worksheet named MySheet?
Try this. Remove all indentation in the following statements:

With DestWb
Set ws = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
End With
wb.Worksheets("MySheet").Range("A1:FA200").Copy
ws.Range("A1").PasteSpecial xlPasteValues

To learn more about macros, our host Allen Wyatt offers the following:
https://store.tips.net/T010157_ExcelTips_The_Macros.html https://usingoffice.com/


2025-09-23 05:27:26

Dan

Thanks for your earlier reply. Sorry, I have only just come back to this.

with the code updated, and the Dim was as Worksheet part moved to the top of the code, i get compilation errors on the With statement. Can you kindly help again, please?

Sub CombineThirdWorksheets()
Dim sPath As String
Dim Filename As String
Dim wb As Workbook
Dim ws As Worksheet
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)


        With DestWb
            Set ws = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
        End With
        wb.Worksheets("MySheet").Range("A1:FA200").Copy
        ws.Range("A1").PasteSpecial xlPasteValues

' 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


2025-07-27 11:05:35

J. Woolley

@Dan
I'm not sure what you want to do, but here's how to copy a range from a worksheet named 'MySheet' in the wb workbook to a new worksheet in the DestWb workbook using xlPasteValues.
Replace the following statements in the Tip's CombineThirdWorksheets macro
        ' 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
with these statements
        Dim ws As Worksheet
        With DestWb
            Set ws = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
        End With
        wb.Worksheets("MySheet").Range("A1:FA200").Copy
        ws.Range("A1").PasteSpecial xlPasteValues
To learn more about macros, our host Allen Wyatt offers the following:
https://store.tips.net/T010157_ExcelTips_The_Macros.html https://usingoffice.com/


2025-07-25 12:03:26

Dan

Thanks for this. Can I ask about changing part of it, please? If instead of copying the third worksheet I wanted to copy a worksheet with a standardised name, and break the links (probably by paste valuing the data), how would I do that?

So far I have changed your code:
If wb.WorkSheets.Count >= 3 Then
wb.WorkSheets(3).Copy After:=DestWb.Sheets(DestWb.Sheets.Count)

To:
ws.CashflowCQC.Range("A1:FA200").Copy After:=DestWb.Sheets(DestWb.Sheets.Count).Range("A1:FA200")PasteSpecial Paste:=xlPasteValues

It is not working. Apologies, I have a lot to learn here.
Thanks,
Dan


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.