2017-01-05 204 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? –