英文:
Compile value from each workbook to main workbook
问题
我有多个工作簿,每个工作簿都有不同的日期,例如Workbook_20230505,格式如下:
员工ID | 时间 | 员工 |
---|---|---|
34565 | 9:00:35 AM | SYSTEM |
23586 | 5:32:05 AM | SYSTEM |
46354 | 4:15:35 AM | ALEX |
46546 | 4:09:30 AM | CLARE |
98744 | 2:54:18 AM | JOHN |
34534 | 3:23:10 AM | SANDY |
87675 | 4:32:09 AM | MANDA |
35645 | 7:15:23 AM | VOID |
23423 | 6:15:23 AM | ALEX |
23423 | 3:46:15 AM | KEN |
34564 | 7:08:23 AM | KEAT |
我能够根据特定条件使用以下公式获取最新时间:=MAXIFS(B3:B13, C3:C13, "<>SYSTEM", C3:C13, "<>VOID")
如何将结果编译到主文件中,如下所示:
日期 | 最新时间 |
---|---|
05/05/2023 | |
06/05/2023 | |
08/05/2023 |
我尝试了以下方法,但似乎难以使其动态:
ActiveSheet.Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=MAXIFS([Workbook_20230505.xlsx]Sheet1!R3C2:R13C2,[Workbook_20230505.xlsx]Sheet1!R3C4:R13C4,""<>SYSTEM"",&[Workbook_20230505.xlsx]Sheet1!R3C4:R13C4,""<>VOID"")"
我还尝试了以下方法,但遇到了错误:
strPath = "C:\Users\...\Workbook_20230505.xlsx"
Set wb = Workbooks.Open(strPath)
Set ws = wb.Worksheets("Sheet1")
Set sheet = ActiveSheet
Set rng1 = ws.Range("B3:B13")
Set rng2 = ws.Range("C3:C13")
sheet.Range("B2").Select
'ActiveCell.FormulaR1C1 = _
'"=MAXIFS(rng1, rng2, "<>SYSTEM", rng2, "<>VOID")"
最终,我希望使用for循环来获取每个日期的结果。感谢您的帮助!
英文:
I have multiple workbooks for each date e.g. Workbook_20230505 with format as below:
Staff ID | Time | Staff |
---|---|---|
34565 | 9:00:35 AM | SYSTEM |
23586 | 5:32:05 AM | SYSTEM |
46354 | 4:15:35 AM | ALEX |
46546 | 4:09:30 AM | CLARE |
98744 | 2:54:18 AM | JOHN |
34534 | 3:23:10 AM | SANDY |
87675 | 4:32:09 AM | MANDA |
35645 | 7:15:23 AM | VOID |
23423 | 6:15:23 AM | ALEX |
23423 | 3:46:15 AM | KEN |
34564 | 7:08:23 AM | KEAT |
I am able to obtain latest time given a criteria using the formula =MAXIFS(B3:B13, C3:C13, "<>SYSTEM", C3:C13, "<>VOID")
How do I compile the result in a main file as below?
Date | Latest Time |
---|---|
05/05/2023 | |
06/05/2023 | |
08/05/2023 |
I tried using the below, but it seems hard to make it dynamic:
ActiveSheet.Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=MAXIFS([Workbook_20230505.xlsx]Sheet1!R3C2:R13C2,[Workbook_20230505.xlsx]Sheet1!R3C4:R13C4,""<>SYSTEM"",[Workbook_20230505.xlsx]Sheet1!R3C4:R13C4,""<>VOID"")"
I am also trying something as below, however, I am obtaining error:
strPath = "C:\Users\...\Workbook_20230505.xlsx"
Set wb = Workbooks.Open(strPath)
Set ws = wb.Worksheets("Sheet1")
Set sheet = ActiveSheet
Set rng1 = ws.Range("B3:B13")
Set rng2 = ws.Range("C3:C13")
sheet.Range("B2").Select
'ActiveCell.FormulaR1C1 = _
'"=MAXIFS(rng1, rng2, "<>SYStem", rng2, "<>VOID")"
Ultimately, I want to use a for loop to obtain results for each date.
Appreciate the help!
答案1
得分: 0
替代使用 WorksheetFunction.MaxIfs
,你可以使用一个 For
循环来获得所需的结果。
Sub GetMax()
Dim FolderPath As String
Dim FileName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim MaxValue As Date
Dim DatePart As String
Dim LastRow As Long
Dim arr
Application.ScreenUpdating = False
' 更新你的文件夹路径
FolderPath = "D:\Temp\"
' 清除活动工作表并设置格式
With ActiveSheet
.UsedRange.Clear
.Range("A1:B1").Value = Array("Date", "LatestTime")
.Columns(1).NumberFormat = "mm/dd/yyyy"
.Columns(2).NumberFormat = "h:mm:ss AM/PM"
End With
' 检索文件名匹配 "Workbook_*.xlsx" 的文件
FileName = Dir(FolderPath & "Workbook_*.xlsx")
Do While FileName <> ""
DatePart = Split(FileName, "_")(1)
Set wb = Workbooks.Open(FolderPath & FileName)
Set ws = wb.Sheets(1)
' 获取列B的最大值
arr = ws.UsedRange.Value
MaxValue = 0
If UBound(arr, 2) >= 3 Then
For i = 2 To UBound(arr)
If InStr("SYSTEM|VOID", UCase(arr(i, 3))) = 0 Then
If MaxValue < arr(i, 2) Then MaxValue = arr(i, 2)
End If
Next
End If
wb.Close SaveChanges:=False
With ThisWorkbook.Sheets(1)
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If LastRow > 1 Or .Cells(LastRow, 1) <> "" Then LastRow = LastRow + 1
.Cells(LastRow, "A").Value = DateSerial(CInt(Mid(DatePart, 1, 4)), CInt(Mid(DatePart, 5, 2)), CInt(Mid(DatePart, 7, 2)))
.Cells(LastRow, "B").Value = MaxValue
End With
FileName = Dir
Loop
Application.ScreenUpdating = True
End Sub
英文:
Instead of using WorksheetFunction.MaxIfs
, you can get the desired result using a For
loop.
Sub GetMax()
Dim FolderPath As String
Dim FileName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim MaxValue As Date
Dim DatePart As String
Dim LastRow As Long
Dim arr
Application.ScreenUpdating = False
' Update your folder name
FolderPath = "D:\Temp\"
' Clean activesheet and set format
With ActiveSheet
.UsedRange.Clear
.[A1:B1].Value = Array("Date", "LatestTime")
.Columns(1).NumberFormat = "mm/dd/yyyy"
.Columns(2).NumberFormat = "h:mm:ss AM/PM"
End With
' Retrieve files with "Workbook_*.xlsx"
FileName = Dir(FolderPath & "Workbook_*.xlsx")
Do While FileName <> ""
DatePart = Split(FileName, "_")(1)
Set wb = Workbooks.Open(FolderPath & FileName)
Set ws = wb.Sheets(1)
' Get max value of column B
arr = ws.UsedRange.Value
MaxValue = 0
If UBound(arr, 2) >= 3 Then
For i = 2 To UBound(arr)
If InStr("SYSTEM|VOID", UCase(arr(i, 3))) = 0 Then
If MaxValue < arr(i, 2) Then MaxValue = arr(i, 2)
End If
Next
End If
wb.Close SaveChanges:=False
With ThisWorkbook.Sheets(1)
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
If LastRow > 1 Or .Cells(LastRow, 1) <> "" Then LastRow = LastRow + 1
.Cells(LastRow, "A").Value = DateSerial(CInt(Mid(DatePart, 1, 4)), CInt(Mid(DatePart, 5, 2)), CInt(Mid(DatePart, 7, 2)))
.Cells(LastRow, "B").Value = MaxValue
End With
FileName = Dir
Loop
Application.ScreenUpdating = True
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论