小编Per*_*gas的帖子

如果单元格具有背景颜色,则删除整行

我试图删除列D中的单元格具有背景颜色的所有行.我已经编写了下面的代码,但每次运行它时,它都会无限期地运行,直到它最终崩溃.这两个ws1lastrow2的明确界定(我提到它来清除的,为什么我的宏不运行这种可能性)

With ws1
    lastrow2 = ws1.Range("A" & Rows.Count).End(xlUp).Row

    For i = lastrow2 To 2 Step -1
        nodel = False

        If .Cells(i, "D").Interior.ColorIndex = 0 Then
            nodel = True
        End If

        If Not nodel Then
            .Rows(i).EntireRow.Delete
        End If
    Next i
End With
Run Code Online (Sandbox Code Playgroud)

excel vba excel-vba

2
推荐指数
1
解决办法
38
查看次数

如何根据单元格背景颜色删除行

我已经将一系列单元格从一张纸复制并粘贴到我想要编辑的单元格中.该范围的单元格具有行和列(当然).我希望宏做的是通过D列并检查单元格背景颜色.如果除了白色之外还有背景颜色,我希望宏删除该单元所属的整行.因此,作为最终结果,我希望宏只保留D列中的单元格没有填充或白色背景颜色的行.下面提供的代码按照预期执行该任务,但需要花费很多时间.宏起诉的总行数为700.

我提供了迄今为止我使用过的两种不同类型的代码.它们都需要这么长时间.

代码1

With ws1
lastrow2 = ws1.Range("A" & Rows.Count).End(xlUp).Row
For i = lastrow2 To 2 Step -1
nodel = False
If .Cells(i, "D").Interior.ColorIndex = 2 Then
nodel = True
End If
If .Cells(i, "D").Interior.ColorIndex = -4142 Then
nodel = True
End If
If Not nodel Then
.Rows(i).EntireRow.Delete
End If
Next i
End With
Run Code Online (Sandbox Code Playgroud)

代码2

lastrow2 = ws1.Range("A" & Rows.Count).End(xlUp).Row
For Each cell In ws1.Range("D2:D" & lastrow2)
    If Not cell.Interior.ColorIndex = 2 Or cell.Interior.ColorIndex = -4142 Then
        If DeleteRange Is …
Run Code Online (Sandbox Code Playgroud)

excel vba excel-vba

0
推荐指数
1
解决办法
365
查看次数

标签 统计

excel ×2

excel-vba ×2

vba ×2