2017-04-21 73 views
0

我有一个vba创建的电子表格,包含4组标准。我需要根据是否符合所有条件来突出显示表格底部的名称。Excel VBA - 基于许多标准的条件突出显示

如果分析师每天休息91分钟或更少(B3:F9),茶歇休息15分钟或更短(B12:F18),并且至少每次打3个外拨电话,我需要突出显示该名称(如果工作人员的时间是8小时58分钟或更长时间(如果不是这样,则3次通话阈值不适用))。

因此,一个函数将是这样的:

如果

TTLB < 91分钟& TEAB

&如果

STFT <八点58分00秒忽视OBC

如果

STFT> 8时58分00秒& OBC> = 3

高亮(在A22分析师名:A28)

我知道这可能会涉及嵌套循环或两个,我只是不知道从哪里开始。计算“Total Minutes Owed”的循环在下面可能会被修改,以帮助我开始使用它。

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 

enter image description here

回答

1

我敢舒尔,一个更紧凑的代码可以做到的。但是,由于在过去四个小时内没有人回答你,所以至少应该尝试以下内容作为开始。

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