2011-10-05 15 views
1

的「B」列具有的時間occurence在it.Like這些需要一個更好的算法(的時間Occurence)

A    B 
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分鐘內發生隨之行,並與color.Take強調他們看看前兩排,他們發生內五分列中的值是相同的兩個,但第3行相比第一排,當5分鐘後發生,因此,該行應突出。而回來的最後2行,他們也發生在5分鐘內時被忽略,他們應該在5分鐘內發生顏色時可以增加顏色。我想你得到了我想要做的事情。有任何問題請發表評論,我會以更簡潔的方式解釋它。

我的方法:

這是我曾嘗試 ,我用劈一些東西像這些

temp = split(text," ") 

,然後比較溫度(0)和溫度(1)和溫度(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 

這是我done.Im沒有得到任何更多更好的想法去做。我想這樣做可能有其他一些最簡單的方法。可能是其即時通訊不知道所以一些內置的功能,讓我知道,這是唯一的更好的方法去做或任何其他更好的方法或算法來做到這一點? ?像更快的方式做到這一點,請幫我這個

+0

相同的值,(1)應該是連續的?或者它已經按順序排列了? (2)是否足以檢查排列順序? (3)如你的例子所示,你想要的行hightlight?row1('8/4/2011 3:12:07 AM')或row2('8/4/2011 3:15:07 AM') –

+0

列A和列B按升序排序,它們將在升序 – niko

+0

但我們仍然必須檢查列A在後續行中是否具有相同的值,查看第4行和第5行,然後列A值是差異,即使它們在5分鐘內發生,它們也不應該突出顯示 – niko

回答

1

這是你需要的,如果你需要在這裏的任何澄清或更改註釋

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 
0

這裏的代碼算法:

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#

0

首先,它會很好以確保B列中的日期時間值格式正確。要做到這一點:

  1. B列中選擇所有值
  2. 現在按CTRL + 1
  3. 選擇Custom和類型dd/mm/yyyy hh:mm:ss AM/PM

現在,你可以通過所有使用下面的代碼迴路ID在列A和以紅色突出顯示哪些具有相同的ID,且5分鐘內彼此:

Sub WithinFiveMinutes() 
    Dim rngID As Range, id As Range, timeDiff As Long 

    Set rngID = Range("A1:A11") //Change for your id list e.g. 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 
01在列A點
相關問題