需要一个更好的algorithm(时间的发生)

“B”栏中有时间出现。像这样

AB X4T00289 8/4/2011 3:12:07 AM X4T00289 8/4/2011 3:15:07 AM X4T00289 8/4/2011 3:18:20 AM X4T00290 8/4/2011 3:12:37 PM YCE00194 8/8/2011 5:12:17 AM YCE00194 8/8/2011 5:14:07 AM YCE00194 8/10/2011 10:12:06 PM YCE00194 8/10/2011 10:15:16 AM Z4W00109 8/12/2011 11:12:22 AM Z4W00109 8/4/2011 11:58:07 AM Z4W00109 8/4/2011 12:00:07 PM 

我已经采取了一个变种,像这些倾销的范围

 var = activesheet.range("A1:B4000").value 

题:

问题是,我必须识别在A列中具有相同ID并且在5分钟内出现的并且用颜色突出显示它们的后续行。查看前2行,它们在5分钟内出现,并且列A值相同与第一行相比,第三行发生在5分钟之后,所以在突出显示时该行应该被忽略。当返回到最后2行时,它们也在5分钟内出现,当它们出现在5分钟。 我想你得到了我想做的事情。 有任何问题请发表评论,我会更清楚地解释它的方式。

我的方法:

这就是我所尝试过的,我曾经使用过这样的东西

 temp = split(text," ") 

然后比较temp(0)和temp(1)和temp(2)以及随后的行

 temp(0) it has year date and month in it temp(1) it has Time temp(2) it has AM or PM if temp(2) and temp(0) are equal for conesequent rows then this piece of code executes temp_var=split(temp(1),":") again again temp_var has temp_var(0)=hours temp_var(1)=minutes temp_var=seconds Now I have to check hours if hours are equal then I have to check for minutes like (minutes - next row minutes) <= 5 then color it 

这就是我所做的,没有更好的想法去做。 我想这样做可能还有一些其他最简单的方法。 可能是一些内置函数,我不知道所以让我知道这是唯一的更好的方法来做或任何其他更好的方法或algorithm来做到这一点? 像更快的方式来做到这一点,请帮助我

这是你需要的代码,如果你需要澄清或改变评论在这里

 Sub HighlightDiff() Dim r As Integer Dim i As Integer Dim diff As Integer Dim y As Integer Dim m As Integer Dim d As Integer Dim h As Integer r = 4000 ' Total No. of rows For i = 1 To r If (Trim(Cells(i, 1).Value) = Trim(Cells(i + 1, 1).Value)) Then 'd = Cells(i, 2).Value - Cells(i + 1, 2).Value y = Year(Sheet1.Cells(i, 2)) - Year(Sheet1.Cells(i + 1, 2)) m = Month(Sheet1.Cells(i, 2)) - Month(Sheet1.Cells(i + 1, 2)) d = Day(Sheet1.Cells(i, 2)) - Day(Sheet1.Cells(i + 1, 2)) 'h = Hour(Sheet1.Cells(i, 2)) - Hour(Sheet1.Cells(i + 1, 2)) If ((y + m + d) = 0) Then diff = (Hour(Sheet1.Cells(i, 2)) * 60 + Minute(Sheet1.Cells(i, 2))) - (Hour(Sheet1.Cells(i + 1, 2)) * 60 + Minute(Sheet1.Cells(i + 1, 2))) If (diff > -5 And diff < 5) Then Range(Cells(i, 1), Cells(i, 2)).Interior.ColorIndex = 3 End If End If End If Next i End Sub 

这里是algorithm:

 for each c in col B minTime = MIN(col b where ref = current ref) if c-minTime < 5 min then change background end if next c 

请注意,你可以像这样获得dateTime差异:
if range("onecell")-range("anothercell") < #00:05#

首先,确保B列中的date时间值的格式正确无误。 去做这个:

  1. select列B中的所有值
  2. 现在按CTRL + 1
  3. selectCustom并inputdd/mm/yyyy hh:mm:ss AM/PM

现在你可以使用下面的代码循环遍历A列中的所有id,并用红色突出显示哪些id具有相同的id,并且在5分钟之内:

 Sub WithinFiveMinutes() Dim rngID As Range, id As Range, timeDiff As Long Set rngID = Range("A1:A11") //Change for your id list eg A1:A4000 For Each id In rngID If id = id.Offset(1, 0) Then timeDiff = DateDiff("n", CDate(id.Offset(0, 1)), CDate(id.Offset(1, 1))) //"n" gives time difference in minutes... If timeDiff >= -5 And timeDiff <= 5 Then Range(id, id.Offset(0, 1)).Interior.ColorIndex = 3 Range(id.Offset(1, 0), id.Offset(1, 1)).Interior.ColorIndex = 3 End If End If Next id End Sub