2017-03-06 75 views
0

我目前正在研究一種「警告系統」,它會在滿足約束條件時觸發消息框。例如,如果在列A中找到五次名稱「Bob」,它將檢查列B以查看是否滿足任何其他約束條件,例如超過5次出現「召喚」。最後,如果滿足這些約束條件,它將查看第I列,以查看這些事件中是否有至少兩個不是例外(「否」)。但是,我很難決定如何設置它。我想過使用一系列If語句,像這樣:搜索相鄰列中的行

Sub WarningSystem() 
Dim SMNameCounter As Integer 
Dim CategoryCounter As Integer 
Dim ExceptionCounter As Integer 
SMNameCounter = Application.WorksheetFunction.CountIf(Range("A1:A200"), "Bob") 
If SMNameCounter = "2" Then 
CategoryCounter = Application.WorksheetFunction.CountIf(Range("B1:B200"), "Call Out") 
    If CategoryCounter = "2" Then 
     ExceptionCounter = Application.WorksheetFunction.CountIf(Range("I1:I200"), "No") 
      If ExceptionCounter = "2" Then 
       MsgBox "Warning! Bob has missed more than (" & CategoryCounter & ") days in a row. One more occurence will result in consequence" 
      Else ' Do Nothing/No Trigger 
      End If 
    Else ' Do Nothing/No Trigger 
    End If 
Else ' Do Nothing/No Trigger 
End If 
End Sub 

這看起來是非常繁瑣和難以重新創建許多變量。任何人都可以指出我正確的方向,我應該如何設置?

謝謝!

+0

你的意思是,如果A200 Bob在列A中,你想檢查同一行的col B和col I,或者任何行?你可能想考慮一個數據透視表。 – SJR

+0

是的,在同一行檢查它們。我應該提到這在我的初始代碼中也是一個明顯的錯誤 - 它只是查看列的每個單元格,而不是像我希望的那樣對應的行。但是,我不太確定如何使用我試圖編寫它的方式來指定該範圍 – dwirony

+0

爲什麼在VBA中這樣做?看起來你可以做一個嵌套的'IF'函數調用來獲得相同的信息,並使本地化的顯式行參考。 – GoldBishop

回答

0

您可能需要使用CountIfs的內部計數:

Sub WarningSystem() 
    Dim SMNameCounter As Integer 
    Dim CategoryCounter As Integer 
    Dim ExceptionCounter As Integer 
    SMNameCounter = Application.WorksheetFunction.CountIf(Range("A1:A200"), "Bob") 
    If SMNameCounter = 5 Then 
     CategoryCounter = Application.WorksheetFunction.CountIfs(Range("A1:A200"), "Bob", _ 
                   Range("B1:B200"), "Call Out") 
     If CategoryCounter > 5 Then 
      ExceptionCounter = Application.WorksheetFunction.CountIfs(Range("A1:A200"), "Bob", _ 
                     Range("B1:B200"), "Call Out", _ 
                     Range("I1:I200"), "No") 
      If ExceptionCounter >= 2 Then 
       MsgBox "Warning! Bob has missed more than (" & CategoryCounter & ") days in a row. One more occurence will result in consequence" 
      End If 
     End If 
    End If 
End Sub 

編輯生成在A1中發現的任何名稱的錯誤消息:

Sub WarningSystem() 
    Dim SMNameCounter As Integer 
    Dim CategoryCounter As Integer 
    Dim ExceptionCounter As Integer 
    Dim r As Long 
    For r = 1 To 200 
     'Count how many times the current value in column A has occurred up 
     'to and including this row 
     SMNameCounter = Application.WorksheetFunction.CountIf(Range("A1:A" & r), Cells(r, "A").Value) 
     If SMNameCounter = 1 Then 
      'Only do the rest of the processing if this was the first occurrence 
      SMNameCounter = Application.WorksheetFunction.CountIf(Range("A1:A200"), Cells(r, "A").Value) 
      If SMNameCounter = 5 Then 
       CategoryCounter = Application.WorksheetFunction.CountIfs(Range("A1:A200"), Cells(r, "A").Value, _ 
                     Range("B1:B200"), "Call Out") 
       If CategoryCounter > 5 Then 
        ExceptionCounter = Application.WorksheetFunction.CountIfs(Range("A1:A200"), Cells(r, "A").Value, _ 
                       Range("B1:B200"), "Call Out", _ 
                       Range("I1:I200"), "No") 
        If ExceptionCounter >= 2 Then 
         MsgBox "Warning! " & Cells(r, "A").Value & " has missed more than (" & CategoryCounter & ") days in a row. One more occurrence will result in consequence" 
        End If 
       End If 
      End If 
     End If 
    Next 
End Sub 
+0

這是一個完美的解決方案 - 我需要某種計數功能。最後,我怎麼才能找到任何名字,不一定是「鮑勃」?我需要設置一些通用變量。因此,如果它看到任何名稱超過5次,它會檢查是否有5個呼叫等。 – dwirony

+0

@dwirony - 回答編輯 – YowE3K

+0

再次感謝YowE3k。通過添加一些ElseIfs,我能夠覆蓋(大部分)所需的場景。感謝幫助 – dwirony