【问题标题】:Excel: check time difference depending on cell valuesExcel:根据单元格值检查时差
【发布时间】:2013-04-07 19:32:33
【问题描述】:

我在 excel 中有两列,例如:

BoxA1000 | 7/4/2013 15:00:43 - User
BoxA1001 | 7/4/2013 15:01:43 - User
BoxA1002 | 7/4/2013 15:02:43 - User
BoxA1003 | 7/4/2013 15:03:43 - User
BoxA1000 | 7/4/2013 15:04:43 - User

对于每一行,我想查找是否有前一行,其中第一列的单元格包含相同的值(BoxA1000),然后查看第二列单元格的时间差是多少并将其放在新的单元格中在第三行。

对于上面的例子,我希望结果是

BoxA1000 | 7/4/2013 15:00:43 - User | NO previous entry
BoxA1001 | 7/4/2013 15:01:43 - User | NO previous entry
BoxA1002 | 7/4/2013 15:02:43 - User | NO previous entry
BoxA1003 | 7/4/2013 15:03:43 - User | NO previous entry
BoxA1000 | 7/4/2013 15:04:43 - User | 00:04:00 (or) 4 minites (or) something like that

如何使用宏来做到这一点?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    附加假设:没有列标题,数据范围连续并以Range("A1") 开始,任何 A 列条目只有一次可能重复。

    Sub test_Solution()
    
    Dim arrBox As Variant
        arrBox = Range(Range("A1"), Range("B1").End(xlDown))
    Dim boFound As Boolean
    
    Dim i As Long, j As Long
    For i = 1 To UBound(arrBox, 1)
        For j = 1 To i
            If arrBox(i, 1) = arrBox(j, 1) And i <> j Then
                'here we found
                Cells(i, 3) = Format(arrBox(j, 2) - arrBox(i, 2), "h:m:s")
                boFound = True
                Exit For
            End If
        Next j
        If Not boFound Then Cells(i, 3) = "No previous entry"
        boFound = False
    Next i
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 2022-09-22
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多