搜索相邻列中的行

时间:2017-03-06 20:15:00

标签: excel excel-vba vba

我目前正在制作警告系统"在满足约束时将触发消息框的类别。例如,如果名称" Bob"在A列中找到五次,它将检查B列以查看是否满足任何其他约束,例如超过5次出现" Call Out"。最后,如果满足这些约束,它将查看第I列,以查看这些事件中是否至少有2个不是例外(" No")。但是,我很难决定如何设置它。我想过使用一系列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

这看起来非常繁琐,很难为许多变量重新创建。任何人都可以指出我应该如何设置它吗?

谢谢!

1 个答案:

答案 0 :(得分: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:A200

中找到的任何名称生成错误消息
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