2017-01-05 202 views
0

規則1:刪除行如果A列中的單元格爲綠色,而J列包含術語「SA註釋 - 」只包含不完全然後刪除行。 然後規則2:刪除列如果A列中的單元格爲紅色,而列J不包含術語「SA註釋 - 」則刪除行。 然後規則3:如果J列中的單元格沒有值,則將術語「Sa註釋 - 」添加到任何沒有值的單元格。刪除具有特定單元格顏色的行 - 有條件格式顏色

這些單元格用條件格式填充爲紅色?

我知道我需要用戶Instr嗎?如果我不在尋找完全匹配。

Sub sbDelete_Rows_Based_On_Cell_Color() 

Dim lRow As Long 
Dim iCntr As Long 

lRow = 9999 
For iCntr = lRow To 1 Step -1 
    If Cells(iCntr, 1).Interior.ColorIndex = xlNone And Cells(iCntr, 10).Value = "SA Comments -" Then 
    '2 = None 
    Rows(iCntr).Delete 

    ElseIf Cells(iCntr, 1).Interior.ColorIndex = 3 And Cells(iCntr, 10).Value <> "SA Comments -" Then 
     '4 = Red 
     Rows(iCntr).Delete 
    End If 

Next iCntr 

End Sub 

回答

1

下面的代碼工作對我來說,只是確保你在小區A列中

不知道具有4 Interior.Color,但你正在尋找有一個精確匹配或部分匹配「SA評論 - 」?

您是否在查找列J中的內部顏色的同時查找J列中的文本?

Private Sub CommandButton21_Click() 

Dim lastrow As Long 

With ThisWorkbook.Worksheets("Outstanding Aged Incidents") 
    ' reading last row with data from Column A 
    lastrow = .Cells(.Rows.Count, 1).End(xlUp).Row 

    For i = lastrow To 2 Step -1    
     If .Cells(i, 10).Value = "SA Comments -" And .Cells(i, 1).Interior.Color = 4 Then 
      .Rows(i).Delete 
     End If   
    Next i 
End With 

End Sub 

編輯1:如果您正在尋找 「SA評論 - 」 的部分比賽,你有2種選擇:

INSTR - 使用下面的一行:

If InStr(.Cells(iCntr, 10).Value, "SA Comments -") > 0 Then 

Li柯 - 使用下面的一行:

If .Cells(iCntr, 10).Value Like "*SA Comments -*" Then 

編輯2:修改後的代碼,以適應自原帖由PO上傳的代碼。

Sub sbDelete_Rows_Based_On_Cell_Color() 

Dim lRow As Long 
Dim iCntr As Long 

lRow = 9999 
For iCntr = lRow To 1 Step -1 
    If Cells(iCntr, 1).Interior.ColorIndex = xlNone And InStr(Cells(iCntr, 10).Value, "SA Comments -") > 0 Then 
    '2 = None 
    Rows(iCntr).Delete 

    ElseIf Cells(iCntr, 1).Interior.ColorIndex = 3 And InStr(Cells(iCntr, 10).Value, "SA Comments -") > 0 Then 
     '4 = Red 
     Rows(iCntr).Delete 
    End If 

Next iCntr 

End Sub 
+0

你好,我不是在尋找一個完全匹配。目前正在尋找解決方案。目前我有以下代碼。 子sbDelete_Rows_Based_On_Cell_Color() 昏暗lRow只要 昏暗iCntr只要 lRow = 9999 對於iCntr = lRow要1個步驟-1 如果單元格(iCntr,1)。 Interior.ColorIndex = xlNone InStr函數和(1,cel.Value, 「SA評論 - 」)然後 「2 =無 行(iCntr).Delete elseif的細胞(iCntr,1).Interior.ColorIndex = 3且單元格(iCntr,10).Value <>「SA註釋 - 」然後 '4 =紅色 行(iCntr)。刪除 結束如果 Next End Sub –

+0

@ D.Mitchell閱讀我添加的答案下**編輯1 ** –

+0

您好,我有無效或無資格的ref? –