I have a master workbook that I need to update 4 sheets daily (Data A, Data B, Data C, Data D)
I get the data workbook and worksheet name differently each time so I change it manually in order for the code run that copies the first sheet of the workbook on a specified folder so I need to change it to copy the used cells only
what should I change the part of "copy only the first sheet of a workbook" part to copy and paste it on the master workbook-specific sheets (the 4 sheets above)? So for example Data1.xlsx first sheet to "Data 1" sheet on master workbook, Data2.xlsx first sheet to "Data 2" sheet on master workbook and so on.
here's my code
I get the data workbook and worksheet name differently each time so I change it manually in order for the code run that copies the first sheet of the workbook on a specified folder so I need to change it to copy the used cells only
what should I change the part of "copy only the first sheet of a workbook" part to copy and paste it on the master workbook-specific sheets (the 4 sheets above)? So for example Data1.xlsx first sheet to "Data 1" sheet on master workbook, Data2.xlsx first sheet to "Data 2" sheet on master workbook and so on.
here's my code
VBA Code:
Sub OpenWorkbooks(sourceFolder As Variant)
Dim sourceFile As String 'Filename obtained by DIR function
Dim masterWb, Sourcewb As Workbook 'Used to loop through each workbook
Dim ext As String
On Error Resume Next
Application.ScreenUpdating = False
Set masterWb = ThisWorkbook
sourceFile = Dir(sourceFolder & "\") 'DIR gets the first file of the folder
Do While sourceFile <> ""
ext = Right(sourceFile, Len(sourceFile) - InStrRev(sourceFile, "."))
If sourceFile <> ThisWorkbook.Name And (ext = "xlsm" Or ext = "xlsx") Then
Workbooks.Open Filename:=sourceFolder & "\" & sourceFile, ReadOnly:=True
Set Sourcewb = ActiveWorkbook
'Copy only the first sheet of a workbook
Sourcewb.Worksheets(1).Copy after:=masterWb.Sheets(masterWb.Worksheets.Count)
'Close the source workbook
Sourcewb.Close SaveChanges:=False
Set Sourcewb = Nothing
End If
sourceFile = Dir 'DIR gets the next file in the folder
Loop
On Error GoTo 0
Application.ScreenUpdating = True
End Sub