0

我在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

我怎么能用宏来做到这一点?

4

1 回答 1

0

附加假设:没有列标题,数据范围连续且开始于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
于 2013-04-07T20:35:08.277 回答