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?
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
[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
Yes, they all have .xlsx extension.Do your files have an “xlsx” extension? Post your revised macro.
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
Thanks! That solved itPath should be: “C:\Test\”