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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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,215,352
Messages
6,124,455
Members
449,161
Latest member
NHOJ

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