gpt4 book ai didi

excel - 在Excel中检索重复项之间最大日期的单元格值

转载 作者:行者123 更新时间:2023-12-04 20:18:23 25 4
gpt4 key购买 nike

我在 Excel 2010 的 Excel 工作表中有以下列:

Batch Number        BatchID            END TIME
120319-0001 120319-0001_TEST1 3/20/12 13:44
120319-0001 120319-0001_TEST2 3/20/12 10:05
120319-0002 120319-0002_TEST1 3/20/12 14:40
120319-0002 120319-0002_TEST2 3/20/12 12:46
120319-0003 120319-0003_TEST1 3/20/12 14:01
120319-0003 120319-0003_TEST2 3/20/12 12:11
120319-0004 120319-0004_TEST1 3/20/12 15:37
120319-0004 120319-0004_TEST2 3/20/12 11:59
120319-0005 120319-0005_TEST1 3/20/12 19:06
120319-0005 120319-0005_TEST2 3/20/12 11:47

我需要一个可以填写在第 4 列中的公式,该公式将执行以下操作:
  • 选择“批号”列中的所有重复项
  • 在“END TIME”列中查找 Max
  • 在所有重复项的“结束时间”中,用“BatchID”列中的单元格值填充第 4 列

  • 第 4 列的预期输出:
    120319-0001_TEST1
    120319-0001_TEST1
    120319-0002_TEST1
    120319-0002_TEST1
    120319-0003_TEST1
    120319-0003_TEST1
    120319-0004_TEST1
    120319-0004_TEST1
    120319-0005_TEST1
    120319-0005_TEST1

    我尝试使用 INDEX 的组合, MATCH , 和 IF声明,还没有让它工作。

    我也愿意使用 VBA/宏解决方案。

    最佳答案

    我快速尝试使用公式,但我认为您可能需要使用数组公式,它们对我来说毫无意义!

    下面的宏应该可以做到。 (虽然比较时间戳可能是一个问题)

    Sub Main()
    Dim Sheet As Worksheet
    Dim Data As Range

    Set Sheet = ThisWorkbook.Worksheets("Sheet1")
    Set Data = Sheet.Range("A2:A" & Range("A" & Range("A" & Sheet.UsedRange.Rows.Count).Row)

    Dim BatchNumber As Variant
    Dim EndTime As Variant
    Dim Result As Variant

    BatchNumber = Data.Value2
    BatchID = Data.Offset(ColumnOffset:=1).Value2
    EndTime = Data.Offset(ColumnOffset:=2).Value2
    Result = Data.Offset(ColumnOffset:=3).Value2

    Dim Index As Integer
    Dim Lookup As Integer
    Dim Max As Integer

    For Index = LBound(BatchNumber, 1) To UBound(BatchNumber, 1)
    Max = Index
    For Lookup = LBound(BatchNumber, 1) To UBound(BatchNumber, 1)
    If BatchNumber(Lookup, 1) = BatchNumber(Index, 1) Then
    If Not Lookup = Index Then
    ' NOTE: you might to do stuff to the date/time comparison below to get it to work correctly
    If EndTime(Lookup, 1) > EndTime(Index, 1) Then
    Max = Lookup
    Else
    Max = Index
    End If
    End If
    End If
    Next Lookup
    Result(Index, 1) = BatchID(Max, 1)
    Next Index

    Data.Offset(ColumnOffset:=3).Value2 = Result

    End Sub

    更新

    下面的应该会快一点,因为它只在数据集上迭代两次。而不是第一个 Rows*Rows 迭代(我认为)。
    Sub Main2()
    Dim Sheet As Worksheet
    Dim Data As Range
    Dim vData As Variant
    Dim vResult As Variant
    Set Sheet = ThisWorkbook.Worksheets("Sheet1")
    Set Data = Sheet.Range("A2:A" & Range("A" & Sheet.UsedRange.Rows.Count).Row)

    vData = Data.Resize(ColumnSize:=3).Value2
    vResult = Data.Value2
    Dim List As Object

    Set List = CreateObject("Scripting.Dictionary")

    Dim Index As Integer
    Dim Key As String

    For Index = LBound(vData, 1) To UBound(vData, 1)
    Key = vData(Index, 1)
    If List.exists(Key) Then
    If vData(Index, 3) > vData(List(Key)(1), 3) Then
    List(Key)(1) = Index
    End If
    Else
    List.Add vData(Index, 1), Array(Index, Index)
    End If
    Next Index

    For Index = LBound(vData, 1) To UBound(vData, 1)
    Key = vData(Index, 1)
    vResult(Index, 1) = vData(List(Key)(1), 2)
    Next Index

    Data.Offset(ColumnOffset:=3).Value2 = vResult

    Set List = Nothing

    End Sub

    关于excel - 在Excel中检索重复项之间最大日期的单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15558500/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com