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
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:
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.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Does your macro need to make sure that the workbook being processed is saved to disk? You can add the saving capability ...
Discover MoreIf you have a workbook with lots of worksheets, you may want those worksheets to be saved off in individual workbooks. ...
Discover MoreExcel allows you to easily make changes to the ribbon, as well as to the Quick Access Toolbar, which is near the ribbon. ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments