查看vba中的周末日期和颜色

时间:2017-02-05 06:19:38

标签: excel vba excel-vba

我需要玩周末日期识别并为col M中的值着色:

  1. 我需要检查表格“延迟”(从第2行开始)的Col K中的“周末日期”

  2. 如果找到周末日期,则在Col O中检查文本“失败”。如果找到了,

  3. 检查Col P中“移动到SA(兼容性减少)”或“文本2”或“文本3”这三个文本中的任何一个。

  4. 如果找到这些文本中的任何一个,并且Col M中的数字> 1,则将其涂成红色。

  5. 我有以下代码仅检查星期日。但是我希望这个周末运行并添加其他关键字来检查。

    Sub SundayCheck()
    
    Dim r, LastRow, RemainingDay As Double
    
    LastRow = Range("M:O").Cells(Rows.count, "A").End(xlUp).Row
    
    Application.ScreenUpdating = False
    
       For r = 2 To LastRow
          RemainingDay = 0
    
            If Weekday(Range("K" & r).Value, vbSunday) = 1 Then
                    RemainingDay = Round((24 - Format(TimeValue(Range("K" & r)), "h")) / 24, 1)
    
                 If InStr(1, Range("O" & r).Text, "Fail", vbTextCompare) > 0    Then
    
                     If Range("M" & r) - RemainingDay >= 1 Then
                         Range("M" & r).Cells.Font.ColorIndex = 3
                     Else
                         Range("M" & r).Cells.Font.ColorIndex = 0
                     End If
    
               End If
            End If
        Next r
         End Sub
    

1 个答案:

答案 0 :(得分:0)

最少编辑代码

Option Explicit

Sub SundayCheck()
    Dim r As Long, LastRow As Long

    LastRow = Cells(Rows.Count, "A").End(xlUp).row
    Application.ScreenUpdating = False
    For r = 2 To LastRow
        If Weekday(Range("K" & r).Value, vbSunday) = 1 Or Weekday(Range("K" & r).Value, vbSunday) = 7 Then
           If UCase(Range("O" & r).Text) = "FAIL" Then
                Select Case True
                    Case InStr(Range("P" & r).Text, "Moved to SA (Compatibility Reduction)") > 0, _
                         InStr(Range("P" & r).Text, "Text2") > 0, _
                         InStr(Range("P" & r).Text, "Text3") > 0
                        If Range("M" & r) > 1 Then
                            Range("M" & r).Cells.Font.ColorIndex = 3
                        Else
                            Range("M" & r).Cells.Font.ColorIndex = 0
                        End If
                End Select
            End If
        End If
    Next r
End Sub

我把你的条件3作为完全匹配而不是部分匹配