2017-02-15 98 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