I have been researching this for weeks and just keep going around in circles, can some please help and save my sanity.
Problem: I have 2 workbooks (Daily report) and (wagon Numbers).
What I'm trying to achieve. miserably I might add, is to run VBA code from the wagon numbers sheet to prompt the user to open a folder location and run through each daily report excel sheet in that folder (which are simple named the days dates e.g 10.05.23.xlsx).
for each file opened sumif/Countifs or sumproduct (not sure which one cause I can't get either to work) multiple criteria and input the values in the wagon numbers workbook on the next blank line within a certain range.
The below sumifs work if I am only referencing cell in the same worksheet but will not carry over when I reference another workbook?
sound easy.... Some code I have as per below.
It does automatically open files and run through each file in the folder but the issue is the month (02 February) will be variable depending, well, on the month, not sure how to manage this?.
The sumifs/countifs will not be transferred to the wagon totals workbook and not positive on what code to ensure the values returned will not overwrite the totals from the previous sheet when I can get it to work without specifying each range in the code (e.g below code should place return value in B1,B2,B3 - I dont want to duplicate the code to have it place the return values in C1,C2,C3, D1 D2 D3 ect.
Any help would be greatly appreciated.
Problem: I have 2 workbooks (Daily report) and (wagon Numbers).
What I'm trying to achieve. miserably I might add, is to run VBA code from the wagon numbers sheet to prompt the user to open a folder location and run through each daily report excel sheet in that folder (which are simple named the days dates e.g 10.05.23.xlsx).
for each file opened sumif/Countifs or sumproduct (not sure which one cause I can't get either to work) multiple criteria and input the values in the wagon numbers workbook on the next blank line within a certain range.
The below sumifs work if I am only referencing cell in the same worksheet but will not carry over when I reference another workbook?
sound easy.... Some code I have as per below.
It does automatically open files and run through each file in the folder but the issue is the month (02 February) will be variable depending, well, on the month, not sure how to manage this?.
The sumifs/countifs will not be transferred to the wagon totals workbook and not positive on what code to ensure the values returned will not overwrite the totals from the previous sheet when I can get it to work without specifying each range in the code (e.g below code should place return value in B1,B2,B3 - I dont want to duplicate the code to have it place the return values in C1,C2,C3, D1 D2 D3 ect.
VBA Code:
Sub OpenAllWorkbooks()
Dim MyFiles As String
Dim dblAnswer As Double
Dim ws2 As Workbook
Application.ScreenUpdating = False
Set ws1 = ActiveSheet - 'workbook that has been opened by user
Set ws2 = Workbooks("wagon totals.xlsx").Worksheets("Wagon count") - 'Master workbook that the code is run from
MyFiles = Dir("G:\testFile\2023\02 February\*.xlsx")
Do While MyFiles <> ""
Workbooks.Open "G:\TestFile\2023\02 February" & MyFiles
ws2.Range("B2") = Application.WorksheetFunction.SumIfs(ws1.Range("H4:H26"), ws1.Range("A4:A26"), "Operator", ws1.Range("B4:B26"), "Product1")
ws2.Range("B1") = WorksheetFunction.CountIfs(Range("A4:A26"), "Operator", Range("B4:B26"), "Product1", Range("H4:H26"), ">0")
ws2.Range("B3")) = WorksheetFunction.SumIfs(Range("H4:H26"), Range("A4:A26"), "Operator", Range("B4:B26"), "Product2")
MsgBox ActiveWorkbook.Name
ActiveWorkbook.Close SaveChanges:=True
MyFiles = Dir
Loop
End Sub
Any help would be greatly appreciated.