2017-04-21 75 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