Sub 檢測()
Dim xD, xD1, R&, T$, TM1&, TM2&, i&
R = [C65536].End(xlUp).Row
[H:H].ClearContents
Set xD = CreateObject("Scripting.Dictionary")
Set xD1 = CreateObject("Scripting.Dictionary")
For i = 2 To R
T = Range("C" & i): TM1 = Int(Range("E" & i)): TM2 = Int(Range("G" & i))
If T = "" Or TM1 = 0 Or TM2 = 0 Then GoTo 101
If TM1 < xD(T) Then xD1(T & TM1) = "到期日未排序": GoTo 101
xD(T) = TM1
If xD(T & TM2) = 0 Then xD(T & TM2) = TM1
If xD(T & TM2) <> TM1 Then xD1(T & TM2) = "到期日異常": GoTo 101
101: Next
For i = 2 To R
T = Range("C" & i): TM1 = Int(Range("E" & i)): TM2 = Int(Range("G" & i))
If xD1(T & TM1) <> "" Then Range("H" & i) = xD1(T & TM1)
If xD1(T & TM2) <> "" Then Range("H" & i) = xD1(T & TM2)
102: Next
End Sub
Sub 檢測1()
Dim xD, xD1, R&, T$, TM1, TM2, i&, TT$
R = [C65536].End(xlUp).Row
[A:B].ClearContents
Set xD = CreateObject("Scripting.Dictionary")
Set xD1 = CreateObject("Scripting.Dictionary")
For i = 2 To R
T = Range("C" & i): TM1 = Range("E" & i): TM2 = Range("G" & i)
If T = "" Or IsDate(TM1) = 0 Or IsDate(TM2) = 0 Then GoTo 101
TM1 = Int(TM1): TM2 = Int(TM2)
If TM1 < xD(T) Then xD1(T & TM1) = "2.到期日未排序"
xD(T) = TM1
If xD(T & TM2) = 0 Then xD(T & TM2) = TM1
If xD(T & TM2) <> TM1 Then xD1(T & TM2) = "1.到期日異常": GoTo 101
101: Next
For i = 2 To R
TT = ""
T = Range("C" & i): TM1 = Range("E" & i): TM2 = Range("G" & i)
If T = "" And TM1 = "" And TM2 = "" Then GoTo 102
If T = "" Then TT = "/1.號碼"
If Not IsDate(TM1) Then TT = TT & "" & "/2.到期日"
If Not IsDate(TM2) Then TT = TT & "/3.製造日"
If TT <> "" Then Range("B" & i) = "*請檢查_" & Mid(TT, 2) & "": GoTo 102
TM1 = Int(TM1): TM2 = Int(TM2)
If xD1(T & TM1) <> "" Then Range("A" & i) = xD1(T & TM1)
If xD1(T & TM2) <> "" Then Range("A" & i) = xD1(T & TM2)
102: Next
End Sub作者: yen956 時間: 2015-12-20 11:35
抱歉!!臨界值沒抓準, 已修改, 請再測試看看!!
'插入序號, 以便恢復原狀
Sub 插入序號(LstR As Integer)
Dim I As Integer
For I = 2 To LstR
Cells(I, 1) = I
Next
End Sub
Sub test()
Dim LstR As Integer, LstR2 As Integer, sR As Integer, I As Integer, cnt As Integer
Dim minDate As Date
[J2:K65536] = ""
'清除底色
[J:K].Interior.ColorIndex = xlNone
LstR2 = Cells(Rows.Count, 3).End(xlUp).Row
插入序號 LstR:=LstR2 '插入序號, 以便恢復原狀
[A1].Resize(LstR2, 10).Sort _
Key1:=[C1], Order1:=xlAscending, _
Key2:=[G1], Order1:=xlAscending, _
Header:=xlYes
LstR = Cells(Rows.Count, 3).End(xlUp).Row
sR = 1
Do
sR = sR + 1
If sR = 2 Then GoTo Next1:
cnt = sR
Do
'規則1.號碼相同且製造日相同, 但到期日不同
If Cells(sR, 3) = Cells(sR - 1, 3) And Int(Cells(sR, 7)) = Int(Cells(sR - 1, 7)) And Cells(sR, 5) <> Cells(sR - 1, 5) Then
Cells(sR - 1, 9) = "到期日異常1"
Cells(sR - 1, 9).Interior.ColorIndex = 8
Cells(sR, 9) = "到期日異常1"
Cells(sR, 9).Interior.ColorIndex = 8
End If
sR = sR + 1
Loop Until Cells(sR, 3) <> Cells(sR - 1, 3) Or sR > LstR '直到 號碼不同 或 資料結尾