Hi All,
I am trying to have code open and run through each workbook in the respective folder and return certain values from the sumifs that have multiple criteria multiple criteria.
Most of the code functions as required but the part I struggle with is having the arrival and departure details on separate worksheets and want them to input in the same worksheet.
the data pulled from sheet 1 ("Arrivals") will be in column "A" (OperatorName), "B" (Product), "C" (Arrival ID), "G" (wagons), "I" (Arrival time).
the data pulled from sheet 2 (Departures" will be in column "C" (DepartureID), "H" (Departure time)
In addition, if the source sheet has multiple instances that match the criteria it adds them and inputs the total value whereas I need it to input each occasion and not sure how to convert the code.
Also, I need to record the date of each respective record in Column "A". This is in Cell "O2" of each workbook.
Question: the time field in the source worksheet is formatted using 'Special - codigo Postal' to prevent having to insert ":" for each time input. any way for VBA to convert this back to a time format e.g 1745 becomes 17:45:00
Below is the code I currently have and a snapshot of the worksheet I need the data to be added to.
Thanks Bedsy.
I am trying to have code open and run through each workbook in the respective folder and return certain values from the sumifs that have multiple criteria multiple criteria.
Most of the code functions as required but the part I struggle with is having the arrival and departure details on separate worksheets and want them to input in the same worksheet.
the data pulled from sheet 1 ("Arrivals") will be in column "A" (OperatorName), "B" (Product), "C" (Arrival ID), "G" (wagons), "I" (Arrival time).
the data pulled from sheet 2 (Departures" will be in column "C" (DepartureID), "H" (Departure time)
In addition, if the source sheet has multiple instances that match the criteria it adds them and inputs the total value whereas I need it to input each occasion and not sure how to convert the code.
Also, I need to record the date of each respective record in Column "A". This is in Cell "O2" of each workbook.
Question: the time field in the source worksheet is formatted using 'Special - codigo Postal' to prevent having to insert ":" for each time input. any way for VBA to convert this back to a time format e.g 1745 becomes 17:45:00
Below is the code I currently have and a snapshot of the worksheet I need the data to be added to.
VBA Code:
Sub OpenBooks()
Dim sPath As String, MyFiles As String
Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
Dim wb2 As Workbook
Dim i As Long
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set sh3 = ThisWorkbook.Sheets("Staged wagons")
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select Folder"
If .Show <> -1 Then Exit Sub
sPath = .SelectedItems(1) & "\"
End With
i = 1 'initial row to put the data
MyFiles = Dir(sPath & "*.xlsx")
Do While MyFiles <> ""
Set wb2 = Workbooks.Open(sPath & MyFiles)
Set sh2 = wb2.Sheets("Arrivals")
With wb2.Sheets("Arrivals").Activate
End With
sh3.Range("B" & i + 2).Value = WorksheetFunction.SumIfs(sh2.Range("OperatorName"), sh2.Range("A4:A26"), "Operator", sh2.Range("B4:B26"), "Product") 'Operator
sh3.Range("C" & i + 2).Value = WorksheetFunction.SumIfs(sh2.Range("G4:G26"), sh2.Range("A4:A26"), "Operator", sh2.Range("B4:B26"), "Product") ' wagons
sh3.Range("E" & i + 2).Value = WorksheetFunction.SumIfs(sh2.Range("C4:C26"), sh2.Range("A4:A26"), "Operator", sh2.Range("B4:B26"), "Product") 'ID arrival
sh3.Range("G" & i + 2).Value = WorksheetFunction.SumIfs(sh2.Range("M4:M26"), sh2.Range("A4:A26"), "Operator", sh2.Range("B4:B26"), "Product") 'actual arrival time
With wb2.Sheets("Departures").Activate
End With
sh3.Range("H" & i + 2).Value = WorksheetFunction.SumIfs(sh2.Range("C4:C26"), sh2.Range("A4:A26"), "Operator", sh2.Range("B4:B26"), "Product") 'ID Depature
sh3.Range("G" & i + 2).Value = WorksheetFunction.SumIfs(sh2.Range("H4:H26"), sh2.Range("A4:A26"), "Operator", sh2.Range("B4:B26"), "Product") 'actual depart time
i = i + 1
wb2.Close False
MyFiles = Dir()
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Thanks Bedsy.