excel - 在一列中查找类似日期然后计算最小值/最大值之间的差异的公式

标签 excel excel-formula

有谁知道拥有 Excel 的最佳方法:

  • 在一列中查找所有类似日期
  • 计算最小日期时间和最大日期时间之间的总时间
  • 将总计放在单独的列中

  • C                                     D 
    8/27/2018 14:02       insert total time for each date here, ie:  8/27 00:05:03
    8/27/2018 14:01     
    8/27/2018 14:01     
    8/27/2018 13:57     
    8/27/2018 8:56      
    8/24/2018 10:01 
    8/24/2018 9:36      
    8/23/2018 6:01  
    8/21/2018 12:32 
    8/21/2018 11:23     
    8/20/2018 14:03 
    8/20/2018 10:54     
    8/17/2018 14:47 
    

    我用了=TEXT(C7-C8, "h:mm:ss") ,但这根本不适合我。我会添加一个 vlookup 吗?

    最佳答案

    此解决方案将要求您在 VBA 中创建 UDF(用户定义函数)。

    本质上,您将循环查找列中的所有匹配日期。在所有匹配的日期,您将修改 minDtmaxDt必要时变量 - 如果新时间低于或高于您当前存储的时间。

    这可能不是最好的解决方案,因为创建一个包含循环的 UDF 会成为一个瓶颈,但这是我目前能够想到的。

    Option Explicit
    
    Function getTimeDiff(col As String, inputDt As Date)
    
        Dim colRng As Range, compDt As Date
        Set colRng = Columns(col)
    
        compDt = Int(inputDt)   '<-- Converts date/time to date only
    
        Dim cel As Range, maxDt As Date, minDt As Date, tmpDt As Date
        maxDt = inputDt
        minDt = inputDt
        For Each cel In colRng.Cells
            If cel = "" Then Exit For
            tmpDt = CDate(cel)
            If Int(tmpDt) = compDt Then
                If tmpDt > maxDt Then 
                    maxDt = tmpDt
                ElseIf tmpDt < minDt Then 
                    minDt = tmpDt
                End If
            End If
        Next cel
    
        getTimeDiff = Format(maxDt - minDt, "h:mm")
    
    End Function
    

    Read here on creating UDFs

    If you are familiar with UDFs, you may skip this section

    • Press Alt + F11 to open up VBA.
    • In the VBE, click Insert > Module
    • Paste the above code into the module
    • Save as a Macro-Enabled Workbook


    保存 UDF 后,您现在可以使用新创建的公式。

    您的日期似乎在 C 列中。因此,如果您的第一个日期在 C2 中, 你的公式 D2将会:
    =getTimeDiff("C", C2)
    

    enter image description here

    关于excel - 在一列中查找类似日期然后计算最小值/最大值之间的差异的公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52047356/

    相关文章:

    android - 如何在android中使用JXL打开Excel文件

    excel - 设置页面格式以按 2 列打印

    excel - 在 Excel 公式中引用动态命名范围

    excel - 按 Excel 或 Google 表格中的值偏移时间单元格

    excel - 从数组中的多个字符串中提取数字,然后求和 - Excel

    excel - 在excel公式中水平更改变量

    c# - 如何定位excel图表x轴文本位置c#

    javascript - 使用javascript将工作表添加到excel文件

    c# - 如何使用 .net 或 vba 在 excel 中使用的范围末尾插入新行?

    excel - 获取具有特定值的行后单元格的值