Copying sheets from multiple workbooks into ThisWorkbook.

Darranimo

Board Regular
Joined
Jan 19, 2022
Messages
52
Office Version
  1. 365
Platform
  1. Windows
I am trying to copy the sheets from every workbook located in a file to the workbook I have open as separate worksheets. I have tried the following code but it also makes copies of the sheets in the workbook that holds the code:
VBA Code:
Sub CallData()
Dim wb As Workbook
Dim FilePath As String
Dim Filename As String
Dim Sheet As Worksheet
Application.ScreenUpdating = False

Set FileDialog = Application.FileDialog(msoFileDialogFolderPicker)
FileDialog.AllowMultiSelect = False
FileDialog.Title = "Select the Excel Files"
If FileDialog.Show <> -1 Then
    Exit Sub
End If

FilePath = FileDialog.SelectedItems(1) & "\"
Filename = Dir(FilePath & "*.xlsm*")

Do While Filename <> ""
    Set wb = Workbooks.Open(Filename:=FilePath & Filename)
    For Each Sheet In wb.Sheets
        If Sheet.Visible = xlSheetVisible Then 'only copy visible sheets
            Sheet.Copy After:=ThisWorkbook.Sheets(1)
        End If
    Next Sheet
    wb.Close
    'Filename = Dir()
Loop
End Sub

Each workbook that it is copying only contains one sheet. I would like these sheets to copied into the "master workbook" after the sheets that exist in the "master workbook". I would really appreciate the help in solving this issue for if it works it will save me a ton of time!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try this
VBA Code:
Sub CallData()
Dim wb2 As Workbook
Dim wb As Workbook
Dim FilePath As String
Dim Filename As String
Dim Sheet As Worksheet
Application.ScreenUpdating = False
Set wb2 = ActiveWorkbook
Set FileDialog = Application.FileDialog(msoFileDialogFolderPicker)
FileDialog.AllowMultiSelect = False
FileDialog.Title = "Select the Excel Files"
If FileDialog.Show <> -1 Then
    Exit Sub
End If

FilePath = FileDialog.SelectedItems(1) & "\"
Filename = Dir(FilePath & "*.xlsm*")

Do While Filename <> ""
    Set wb = Workbooks.Open(Filename:=FilePath & Filename)
    For Each Sheet In wb.Sheets
        If Sheet.Visible = xlSheetVisible Then 'only copy visible sheets
            Sheet.Copy After:=wb2.Sheets(1)
        End If
    Next Sheet
    wb.Close
    Filename = Dir()
Loop
End Sub
 
Last edited:
Upvote 0
Solution
Try this
VBA Code:
Sub CallData()
Dim wb2 As Workbook
Dim wb As Workbook
Dim FilePath As String
Dim Filename As String
Dim Sheet As Worksheet
Application.ScreenUpdating = False
Set wb2 = ActiveWorkbook
Set FileDialog = Application.FileDialog(msoFileDialogFolderPicker)
FileDialog.AllowMultiSelect = False
FileDialog.Title = "Select the Excel Files"
If FileDialog.Show <> -1 Then
    Exit Sub
End If

FilePath = FileDialog.SelectedItems(1) & "\"
Filename = Dir(FilePath & "*.xlsm*")

Do While Filename <> ""
    Set wb = Workbooks.Open(Filename:=FilePath & Filename)
    For Each Sheet In wb.Sheets
        If Sheet.Visible = xlSheetVisible Then 'only copy visible sheets
            Sheet.Copy After:=wb2.Sheets(1)
        End If
    Next Sheet
    wb.Close
    Filename = Dir()
Loop
End Sub
Thank you!!!! Works perfectly!!
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

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