Excel VBA - 基于许多条件的条件突出显示

乏味的

我有一个带有 4 组标准的 vba 创建的电子表格。我需要根据名称是否符合所有标准在工作表底部突出显示名称。

如果分析师每天的总休息时间 (B3:F9) 不超过 91 分钟,茶歇时间不超过 15 分钟 (B12:F18),并且每天至少拨打 3 个外呼电话(前提是员工工作时间为 8 小时 58 分钟或更长时间(如果不是,则不适用 3 次通话阈值))。

所以,一个函数应该是这样的:

如果

TtlB<91 分钟 & TeaB<15

& 如果

StfT <8:58:00 忽略 ObC

否则如果

StfT >8:58:00 & ObC>=3

突出显示(A22:A28 中的分析师姓名)

我知道它可能会涉及一两个嵌套循环,我只是不知道从哪里开始。下面是计算“总欠款分钟数”的循环,可以对其进行修改以帮助我开始使用它。

Dim i As Integer, j As Integer, k As Integer

j = 3
k = 12
For i = 22 To 28
    Range("B" & i) = "=SUM(G" & j & ",G" & k & ")"
    j = j + 1
    k = k + 1
Next i

在此处输入图片说明

精氨酸

我很确定可以完成更紧凑的代码。但是,由于在过去的四个小时内没有人回答您,请至少尝试以下作为开始。

Private Sub CommandButton1_Click()
    Dim oWs As Worksheet
    Dim rAnalysts As Range
    Dim rBreak As Range
    Dim rObC As Range
    Dim rTea As Range
    Dim rST As Range
    Dim rRow As Range
    Dim rIntersection As Range
    Dim rCell As Range


    Set oWs = Worksheets("MyData") 'The worksheet where data resides
    MaxBreakTime = oWs.Cells(1, 7).Value 'The max break time. I set it in cell G1. Change according to your needs

    Set rAnalysts = oWs.Rows("3:9") 'Define the rows for analysts
    Set rBreak = oWs.Range("B:F") 'define the columns where Break data is placed
    '(similarly, set ranges for tea break, etc)

    For Each rRow In rAnalysts.Rows 'for each row in the analyst range
        sAnalystName = oWs.Cells(rRow.Row, 1).Value 'get the name of the analyst
        lBreakTime = 0 'restart this variable to zero
        Set rIntersection = Application.Intersect(rRow, rBreak) ' intersect the row (the analyst) with the columns of the Break range
        If rIntersection Is Nothing Then
            MsgBox "Ranges do not intersect. Something is radically wrong."
        Else
            For Each rCell In rIntersection.Cells 'id est, friday through thursday
                If rCell.Value > MaxBreakTime Then 'if break was longer that stipulated,....
                    lBreakTime = lBreakTime + rCell.Value - MaxBreakTime 'add the excess to the variable
                End If
            Next
        End If
        'write data somewhere (here, 30 rows down from original Analysts range)
        oWs.Cells(rRow.Row + 30, 1) = sAnalystName
        oWs.Cells(rRow.Row + 30, 2) = lBreakTime

        If lBreakTime > 0 Then
            oWs.Cells(rRow.Row + 30, 2).Font.Color = vbGreen
            oWs.Cells(rRow.Row + 30, 2).Interior.Color = vbRed

        End If
    Next

    'Here something similar for Tea break and Outbounds calls
    'Since output is already writen, you can reuse variables like rIntersection or rCell

End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章