将每个工作簿中的数值编译到主工作簿中。

huangapple go评论112阅读模式
英文:

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
    &#39; Update your folder name
    FolderPath = &quot;D:\Temp\&quot;
    &#39; Clean activesheet and set format
    With ActiveSheet
        .UsedRange.Clear
        .[A1:B1].Value = Array(&quot;Date&quot;, &quot;LatestTime&quot;)
        .Columns(1).NumberFormat = &quot;mm/dd/yyyy&quot;
        .Columns(2).NumberFormat = &quot;h:mm:ss AM/PM&quot;
    End With
    &#39; Retrieve files with &quot;Workbook_*.xlsx&quot;
    FileName = Dir(FolderPath &amp; &quot;Workbook_*.xlsx&quot;)
    Do While FileName &lt;&gt; &quot;&quot;
        DatePart = Split(FileName, &quot;_&quot;)(1)
        Set wb = Workbooks.Open(FolderPath &amp; FileName)
        Set ws = wb.Sheets(1)
        &#39; Get max value of column B
        arr = ws.UsedRange.Value
        MaxValue = 0
        If UBound(arr, 2) &gt;= 3 Then
        For i = 2 To UBound(arr)
            If InStr(&quot;SYSTEM|VOID&quot;, UCase(arr(i, 3))) = 0 Then
                If MaxValue &lt; 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, &quot;A&quot;).End(xlUp).Row
            If LastRow &gt; 1 Or .Cells(LastRow, 1) &lt;&gt; &quot;&quot; Then LastRow = LastRow + 1
            .Cells(LastRow, &quot;A&quot;).Value = DateSerial(CInt(Mid(DatePart, 1, 4)), CInt(Mid(DatePart, 5, 2)), CInt(Mid(DatePart, 7, 2)))
            .Cells(LastRow, &quot;B&quot;).Value = MaxValue
        End With
        FileName = Dir
    Loop
    Application.ScreenUpdating = True
End Sub

将每个工作簿中的数值编译到主工作簿中。

huangapple
  • 本文由 发表于 2023年7月23日 20:02:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定