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
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
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