删除具有特定单元格颜色的行 - 条件格式颜色

问题描述 投票:0回答:2

规则 1:删除行 IF A 列中的单元格为绿色且 J 列包含术语“SA Comments -”仅包含不准确的 THEN 删除行。 然后规则 2:删除行如果 A 列中的单元格为红色且 J 列不包含术语“SA 注释 -”,则删除行。 然后规则 3:如果 J 列中的单元格没有值,那么将术语“Sa Comments -”添加到任何没有值的单元格。

这些单元格被条件格式填充为红色?

我知道我需要使用 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
vba excel colors
2个回答
1
投票

下面的代码对我有用,只需确保 A 列的单元格中的

Interior.Color
为 4。

不确定,但您正在寻找“SA Comments -”的完全匹配或部分匹配?

您是否在检查 A 列中的内部颜色的同时在 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 Comments -”的部分匹配,您有2个选择:

Instr - 使用下面的行:

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

喜欢 - 使用下面的行:

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
投票

我试图解决两个问题但没有任何效果 1. 我在 A 到 I 列中有数据。在 A 列中,数据集和时间之间有日期,由彩色行分隔,这些行要么为空,要么包含单词“pf”,但仍然有彩色行。在整个 A 列中,我只希望每个数据集的第一个单元格由包含剩余日期和时间的彩色行分隔,以及每个数据集中包含日期时间的底部单元格,而不会弄乱其余数据。

2. 如果底行包含单词“pf”,或者如果彩色底行和彩色顶行都包含单词“pf”,则删除整个工作表中任意一组两个彩色行之间的所有行。工作表名称是“数据”

© www.soinside.com 2019 - 2024. All rights reserved.