Automatically Add Sheet from Different Workbook from Specified Folder

TheLSD

New Member
Joined
Jan 12, 2022
Messages
33
Office Version
  1. 2010
Platform
  1. Windows
I have a master file and a Data Folder that contains workbooks with the same format.
I also have a VBA code inside the master file that is able to copy the first sheet of all the selected workbook

VBA Code:
Sub Merge_Files_First_Sheet_Only()
    'Merges all files in a folder to a main file.
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

    'Define variables:
    Dim numberOfFilesChosen, I As Integer
    Dim tempFileDialog As FileDialog
    Dim mainWorkbook, sourceWorkbook As Workbook
    Dim tempWorkSheet As Worksheet
    
    Set mainWorkbook = Application.ActiveWorkbook
    Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
    
    'Allow the user to select multiple workbooks
    tempFileDialog.AllowMultiSelect = True
    
    numberOfFilesChosen = tempFileDialog.Show
    
      'Loop through all selected workbooks
    For I = 1 To tempFileDialog.SelectedItems.Count
        
        'Open each workbook
        Workbooks.Open tempFileDialog.SelectedItems(I)
        
        Set sourceWorkbook = ActiveWorkbook
        
        'Copy only the first sheet of a workbook
        sourceWorkbook.Worksheets(1).Copy After:=mainWorkbook.Sheets(mainWorkbook.Worksheets.Count)
        
        'Close the source workbook
        sourceWorkbook.Close
    Next I

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

The code above needs to be executed manually when I open the file (need to press F8 then run the code)
Is there any way to make the code directly run (automatically without pressing F8) whenever I open the master file that contains the macro and copy the first sheet only from Data Folder? It seems that I need to add the folder address and make the code run automatically
Please help

Thank you in advance
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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