返回列表 上一主題 發帖

[發問] 如何利用VBA按鍵,來找出違反規則的號碼。

本帖最後由 yen956 於 2015-12-13 15:26 編輯

如圖:

1.原資料有多筆 到期日<製造日?
是否要增加此判斷?
2. 規則2 有存在的入要嗎?

TOP

試試看:
  1. '插入序號, 以便恢復原狀
  2. Sub 插入序號(LstR As Integer)
  3.     Dim i As Integer
  4.     For i = 2 To LstR
  5.         Cells(i, 1) = i
  6.     Next
  7. End Sub

  8. Sub test()
  9.     Dim LstR As Integer, LstR2 As Integer, sR As Integer
  10.     Dim mDate1 As Date, mDate2 As Date
  11.     [H2:J65536] = ""
  12.     '清除底色, 如你原有底色的需求, 請將下列去除
  13.     [C:I].Interior.ColorIndex = xlNone
  14.     LstR2 = Cells(Rows.Count, 3).End(xlUp).Row
  15.     插入序號 LstR:=LstR2      '插入序號, 以便恢復原狀
  16.     [A1].Resize(LstR2, 10).Sort _
  17.             Key1:=[C1], Order1:=xlAscending, _
  18.             Key2:=[G1], Order1:=xlAscending, _
  19.             Key3:=[E1], Order1:=xlAscending, _
  20.             Header:=xlYes
  21.     LstR = Cells(Rows.Count, 3).End(xlUp).Row
  22.     sR = 2
  23.     mDate1 = DateSerial(Year(Cells(sR, 7)), Month(Cells(sR, 7)), Day(Cells(sR, 7)))
  24.     Do
  25.         '新增異常紀錄,如無此需求,請去除
  26.         If Cells(sR, 5) < mDate1 Then    '到期日<製造日
  27.             Cells(sR, 8) = "到期日<製造日"
  28.            '加入底色, 如你原有底色的需求, 請將下兩列去除
  29.             Cells(sR, 8).Interior.ColorIndex = 38
  30.             Cells(sR, 3).Resize(1, 5).Interior.ColorIndex = 38
  31.         End If
  32.         
  33.         If sR = 2 Then GoTo Next1:
  34. '        '號碼相同,到期日未排序(規則2 似乎沒有存在的必要, 故去除)
  35. '        If Cells(sR, 3) = Cells(sR - 1, 3) And Cells(sR, 5) < Cells(sR - 1, 5) Then
  36. '            Cells(sR, 10) = "到期日未排序"
  37. '           '加入底色, 如你原有底色的需求, 請將下列去除
  38. '            Cells(sR, 3).Resize(1, 5).Interior.ColorIndex = 6
  39. '        End If
  40.         
  41.         '製造日相同, 但到期日不同
  42.         If mDate1 = mDate2 And Cells(sR, 5) <> Cells(sR - 1, 5) Then
  43.             Cells(sR - 1, 9) = "到期日異常"
  44.            '加入底色, 如你原有底色的需求, 請將下兩列去除
  45.             Cells(sR - 1, 9).Interior.ColorIndex = 8
  46.             Cells(sR - 1, 3).Resize(1, 5).Interior.ColorIndex = 8
  47.             Cells(sR, 9) = "到期日異常"
  48.            '加入底色, 如你原有底色的需求, 請將下兩列去除
  49.             Cells(sR, 9).Interior.ColorIndex = 8
  50.             Cells(sR, 3).Resize(1, 5).Interior.ColorIndex = 8
  51.         End If
  52. Next1:
  53.         sR = sR + 1
  54.         mDate1 = DateSerial(Year(Cells(sR - 1, 7)), Month(Cells(sR - 1, 7)), Day(Cells(sR - 1, 7)))
  55.         mDate2 = DateSerial(Year(Cells(sR, 7)), Month(Cells(sR, 7)), Day(Cells(sR, 7)))
  56.     Loop Until sR > LstR
  57.     '恢復原狀, 方便查核
  58.     [A1].Resize(LstR2, 9).Sort _
  59.             Key1:=[A1], Order1:=xlAscending, _
  60.             Header:=xlYes
  61. End Sub
複製代碼

TOP

針對 規則2 修正:
試試看:
  1. '插入序號, 以便恢復原狀
  2. Sub 插入序號(LstR As Integer)
  3.     Dim I As Integer
  4.     For I = 2 To LstR
  5.         Cells(I, 1) = I
  6.     Next
  7. End Sub
  8. Sub test()
  9.     Dim LstR As Integer, LstR2 As Integer, sR As Integer, I As Integer, cnt As Integer
  10.     Dim minDate As Date
  11.     [H2:J65536] = ""
  12.     '清除底色, 如你原有底色的需求, 請將下列去除
  13.     [C:I].Interior.ColorIndex = xlNone
  14.     LstR2 = Cells(Rows.Count, 3).End(xlUp).Row
  15.     插入序號 LstR:=LstR2      '插入序號, 以便恢復原狀
  16.     [A1].Resize(LstR2, 10).Sort _
  17.             Key1:=[C1], Order1:=xlAscending, _
  18.             Key2:=[G1], Order1:=xlAscending, _
  19.             Header:=xlYes
  20.     LstR = Cells(Rows.Count, 3).End(xlUp).Row
  21.     sR = 1
  22.     Do
  23.         cnt = sR
  24.         Do
  25.             sR = sR + 1
  26.             If sR = 2 Then GoTo Next1:
  27.             '規則1.製造日相同, 但到期日不同
  28.             If Int(Cells(sR, 7)) = Int(Cells(sR - 1, 7)) And Cells(sR, 5) <> Cells(sR - 1, 5) Then
  29.                 Cells(sR - 1, 8) = "到期日異常1"
  30.                '加入底色, 如你原有底色的需求, 請將下兩列去除
  31.                 Cells(sR - 1, 8).Interior.ColorIndex = 8
  32.                 Cells(sR - 1, 3).Resize(1, 5).Interior.ColorIndex = 8
  33.                 Cells(sR, 8) = "到期日異常1"
  34.                '加入底色, 如你原有底色的需求, 請將下兩列去除
  35.                 Cells(sR, 8).Interior.ColorIndex = 8
  36.                 Cells(sR, 3).Resize(1, 5).Interior.ColorIndex = 8
  37.             End If
  38.         Loop Until Cells(sR, 3) <> Cells(sR - 1, 3) Or sR > LstR  '直到 號碼不同 或 資料結尾
  39.         '規則2. 到期日也必須排序
  40.         If sR - cnt <= 1 Then GoTo Next1:
  41.         For I = cnt To sR - 2
  42.             minDate = Application.Min(Cells(I + 1, 5).Resize(sR - I - 1, 1))
  43.             If Cells(I, 5) > minDate Then
  44.                 Cells(I, 9) = "到期日異常2"
  45.                '加入底色, 如你原有底色的需求, 請將下兩列去除
  46.                 Cells(I, 9).Interior.ColorIndex = 38
  47.                 Cells(I, 3).Resize(1, 5).Interior.ColorIndex = 38
  48.             End If
  49.         Next
  50. Next1:
  51.     Loop Until sR > LstR  '直到資料結尾
  52.     '恢復原狀, 方便查核
  53.     [A1].Resize(LstR2, 9).Sort _
  54.             Key1:=[A1], Order1:=xlAscending, _
  55.             Header:=xlYes
  56. End Sub
複製代碼
執行結果如下圖:

TOP

Q2:另外A欄的序號用意是?這是一定都會出現嗎?
VBA52列不是說的很清嗎?
如無此需求, 則相關VBACode去悼即可.
Q1:這不是你自己訂的規則2.嗎?如下圖:

test.jpg (1.61 KB)

test.jpg

TOP

本帖最後由 yen956 於 2015-12-19 09:07 編輯

補充說明:
1. 不加入序號可不可以?
插入序號, 以便恢復原狀,
除錯的目的不是打亂原表格,
因原VBA利用Excel 的排序功能,
    [A1].Resize(LstR2, 10).Sort _
            Key1:=[C1], Order1:=xlAscending, _
            Key2:=[G1], Order1:=xlAscending, _
            Header:=xlYes
會打亂原表格, 故排序前先加入序號.
如欄A有重要資料可移到他處, 如欄K
若移到欄K,[K1]可鍵入"序號", 原VBA可改成
    [A1].Resize(LstR2, 11).Sort _
            Key1:=[C1], Order1:=xlAscending, _
            Key2:=[G1], Order1:=xlAscending, _
            Header:=xlYes
及將
'插入序號, 以便恢復原狀
Sub 插入序號(LstR As Integer)
    Dim I As Integer
    For I = 2 To LstR
        Cells(I, 1) = I
    Next
End Sub
改成
    For I = 2 To LstR
        Cells(I, 11) = I
    Next
及將
    '恢復原狀, 方便查核
    [A1].Resize(LstR2, 9).Sort _
            Key1:=[A1], Order1:=xlAscending, _
            Header:=xlYes
改成
    [A1].Resize(LstR2, 11).Sort _
            Key1:=[k1], Order1:=xlAscending, _
            Header:=xlYes
最後加入
    [K1:K65536] = ""
予以清除.

TOP

本帖最後由 yen956 於 2015-12-19 09:34 編輯

回復 9# RCRG
"不過I24似乎不該出現"到期日異常2",查看一下原因似乎yen大把製造日的"時間"也比對進去了!製造日只需比對"日期",後面時間可以忽略"
反正 "製造日" 相同, "到期日" 不同, 就一定會違反 "規則1",
此時要不要 "把製造日的"時間"也比對進去" 就無關緊要了,
這種情形的違反 "規則2" 必 違反 "規則1",
因利用Excel本身的排序功能,
    [A1].Resize(LstR2, 11).Sort _
            Key1:=[C1], Order1:=xlAscending, _
            Key2:=[G1], Order1:=xlAscending, _
            Header:=xlYes
很難避開時間的問題(除非另加補助欄, 先把欄G用公式 =int(G2) 全部去掉時間, 改成日期).

TOP

建議:
1. 另新增一工作表, 內只有號碼、品名(方便輸入有效期限)、有效期限 三欄
2. 號碼是唯一的, 且須由小到大排列(EXCEL 2003必須, 以後版本不清楚), 有效期限以天為單位.
利用進階篩選, 不選重覆, 即可得號碼是唯一的, 操作如下圖:
test.gif
3. 填入有效期限(單位:天), 即三個月應輸入90,
4. 回到主頁(範例說明頁),在 E2 輸入公式, 向下拉
=IF(C2="","",INT(G2)+LOOKUP(C2,Sheet3!A:A,Sheet3!C:C))
(Sheet3 是工作表名稱)
大功告成, 如此保証 "到期日" 絶對不會出錯!!

TOP

抱歉!!臨界值沒抓準, 已修改, 請再測試看看!!
  1. '插入序號, 以便恢復原狀
  2. Sub 插入序號(LstR As Integer)
  3.     Dim I As Integer
  4.     For I = 2 To LstR
  5.         Cells(I, 1) = I
  6.     Next
  7. End Sub
  8. Sub test()
  9.     Dim LstR As Integer, LstR2 As Integer, sR As Integer, I As Integer, cnt As Integer
  10.     Dim minDate As Date
  11.     [J2:K65536] = ""
  12.     '清除底色
  13.     [J:K].Interior.ColorIndex = xlNone
  14.     LstR2 = Cells(Rows.Count, 3).End(xlUp).Row
  15.     插入序號 LstR:=LstR2      '插入序號, 以便恢復原狀
  16.     [A1].Resize(LstR2, 10).Sort _
  17.             Key1:=[C1], Order1:=xlAscending, _
  18.             Key2:=[G1], Order1:=xlAscending, _
  19.             Header:=xlYes
  20.     LstR = Cells(Rows.Count, 3).End(xlUp).Row
  21.     sR = 1
  22.     Do
  23.         sR = sR + 1
  24.         If sR = 2 Then GoTo Next1:
  25.         cnt = sR
  26.         Do
  27.             '規則1.號碼相同且製造日相同, 但到期日不同
  28.             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
  29.                 Cells(sR - 1, 9) = "到期日異常1"
  30.                 Cells(sR - 1, 9).Interior.ColorIndex = 8
  31.                 Cells(sR, 9) = "到期日異常1"
  32.                 Cells(sR, 9).Interior.ColorIndex = 8
  33.             End If
  34.             sR = sR + 1
  35.         Loop Until Cells(sR, 3) <> Cells(sR - 1, 3) Or sR > LstR    '直到 號碼不同 或 資料結尾
  36.         '規則2. 到期日也必須排序
  37.         If sR - cnt <= 1 Then GoTo Next1:
  38.         For I = cnt To sR - 2
  39.             minDate = Application.Min(Cells(I + 1, 5).Resize(sR - I - 1, 1))
  40.             If Cells(I, 5) > minDate Then
  41.                 Cells(I, 10) = "到期日異常2"
  42.                 Cells(I, 10).Interior.ColorIndex = 38
  43.             End If
  44.         Next
  45. Next1:
  46.     Loop Until sR >= LstR  '直到資料結尾
  47.     '恢復原狀, 方便查核
  48.     [A1].Resize(LstR2, 10).Sort _
  49.             Key1:=[A1], Order1:=xlAscending, _
  50.             Header:=xlYes
  51. End Sub
複製代碼
p.s.
從 製造日 精準到"分鐘"看來, 製造日是由電腦自動輸出的,
但 到期日 為何不用 14# F 的建議, 改用公式自動輸入,
以減少人為 Key In 上的錯誤?
其實, 有違反規則1及規則2 的錯誤, 怎可能沒有 到期日<製造日 的人為 Key In 上的錯誤!?
test.gif

TOP

本帖最後由 yen956 於 2015-12-20 11:46 編輯

回復 16# 准提部林
大大抱歉!!來不及用大大新版測試就上傳!!

TOP

回復 19# RCRG
沒錯, 因錯誤訊息改放到處, 第11列忘了改, 將
    [J2:K65536] = ""
改成
    [H2:J65536] = ""
即可.

p.s.
"其實整篇資料都會是用複製過來的,原則上不會有誤KEY"
那就是原稿就錯?!別的單位送過來的?那又何必除錯?

TOP

        靜思自在 : 待人退一步,愛人寬一寸,就會活得很快樂。
返回列表 上一主題