How to loop through excel sheets in same directory and copy all sheets into a single workbook?

XcelNoobster

New Member
Joined
Jun 7, 2022
Messages
40
How would I create a macro that loops through all the excels sheets in a directory and copy all the sheets in a new workbook?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Change the folder path (in red) to suit your needs.
Rich (BB code):
Sub CopySheets()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook, wkbSource As Workbook, ws As Worksheet
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Mario\Forum Help\Test\"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            For Each ws In Sheets
                ws.Copy after:=wkbDest.Sheets(wkbDest.Sheets.Count)
            Next ws
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hello,

I went ahead and copied this macro in a new workbook(Book1), changed the path to where my folder is, ran it and I don't see the files being copied over to the Book1. Have an idea why?


VBA Code:
[CODE=vba]
Sub CopySheets()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook, wkbSource As Workbook, ws As Worksheet
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Test"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            For Each ws In Sheets
                ws.Copy after:=wkbDest.Sheets(wkbDest.Sheets.Count)
            Next ws
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
[/CODE]
 
Upvote 0
Do your files have an “xlsx” extension? Post your revised macro.
 
Upvote 0
Do your files have an “xlsx” extension? Post your revised macro.
Yes, they all have .xlsx extension.
VBA Code:
Sub CopySheets()
    Application.ScreenUpdating = False
    Dim wkbDest As Workbook, wkbSource As Workbook, ws As Worksheet
    Set wkbDest = ThisWorkbook
    Const strPath As String = "C:\Test"
    ChDir strPath
    strExtension = Dir(strPath & "*.xlsx")
    Do While strExtension <> ""
        Set wkbSource = Workbooks.Open(strPath & strExtension)
        With wkbSource
            For Each ws In Sheets
                ws.Copy after:=wkbDest.Sheets(wkbDest.Sheets.Count)
            Next ws
            .Close savechanges:=False
        End With
        strExtension = Dir
    Loop
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,152
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top