2017-02-15 214 views
0

我有下面的代码检查列P以查找某些文本,然后检查列M是否大于1,如果匹配,那么列M中的数字将被着色。在vba中添加多个if条件

我只想补充一个条件在这里:

-Additionally检查山口Ø文本“失败”,如果匹配,则检查山口P代表提到的字符串,然后检查上校男,如果在数COl M大于3,然后在读取时对其进行着色(只有在Col P中有字符串时)或者不着色。

Sub Test() 
    Dim r As Long, LastRow As Long 
    Dim RemainingDay As Double '<--| 

    With Worksheets("Latency") '<--| reference worksheet "Latency" 
     LastRow = .Cells(.Rows.Count, "A").End(xlUp).row '<--| get row index of its column A last not empty cell 
     Application.ScreenUpdating = False 
     For r = 2 To LastRow 
      RemainingDay = 0 '<--| 

      If Weekday(.Range("K" & r).value, vbSaturday) > 2 Then '<--| having 'Weekday()' function starting from "Saturday", 
       Select Case True 
        Case InStr(.Range("P" & r).text, "Moved to SA (Compatibility Reduction)") > 0, _ 
         InStr(.Range("P" & r).text, "Moved to SA (Failure)") > 0, _ 
         InStr(Range("P" & r).text, "Gold framing") > 0 
         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 Select 
      End If 
     Next r 
    End With 
End Sub 

回答

0

您可以添加新的If在2个选择:

选项1:在一个新的生产线,该Select Case(我的偏好)前。

Sub Test() 
    Dim r As Long, LastRow As Long 
    Dim RemainingDay As Double '<--| 

    With Worksheets("Latency") '<--| reference worksheet "Latency" 
     LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<--| get row index of its column A last not empty cell 
     Application.ScreenUpdating = False 
     For r = 2 To LastRow 
      RemainingDay = 0 '<--| 

      If Weekday(.Range("K" & r).Value, vbSaturday) > 2 Then '<--| having 'Weekday()' function starting from "Saturday", 
       If .Range("o" & r).Value Like "Fail" Then '<-- ****** add the If here ****** 
        Select Case True 
         Case InStr(.Range("P" & r).Text, "Moved to SA (Compatibility Reduction)") > 0, _ 
          InStr(.Range("P" & r).Text, "Moved to SA (Failure)") > 0, _ 
          InStr(Range("P" & r).Text, "Gold framing") > 0 
          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 Select 
       End If 
      End If 
     Next r 
    End With 
End Sub 

选项2:将其添加到在它的If现有线路,使用And

Sub Test() 
    Dim r As Long, LastRow As Long 
    Dim RemainingDay As Double '<--| 

    With Worksheets("Latency") '<--| reference worksheet "Latency" 
     LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row '<--| get row index of its column A last not empty cell 
     Application.ScreenUpdating = False 
     For r = 2 To LastRow 
      RemainingDay = 0 '<--| 

      ' ****** add the extra if at the line below with an And ***** 
      If Weekday(.Range("K" & r).Value, vbSaturday) > 2 And .Range("o" & r).Value Like "Fail" Then '<--| having 'Weekday()' function starting from "Saturday", 
       Select Case True 
        Case InStr(.Range("P" & r).Text, "Moved to SA (Compatibility Reduction)") > 0, _ 
         InStr(.Range("P" & r).Text, "Moved to SA (Failure)") > 0, _ 
         InStr(Range("P" & r).Text, "Gold framing") > 0 
         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 Select 
      End If 
     Next r 
    End With 
End Sub