简体   繁体   English

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

[英]How to delete rows based on cell background color?

I copy and paste a range of cells from one sheet to one I want to edit.我将一系列单元格从一张纸复制并粘贴到我想要编辑的一张纸上。

I want to go through column D and check each cell's background color.我想通过 D 列并检查每个单元格的背景颜色。 If there is a color besides white, I want to delete the entire row that the cell belongs to.如果除了白色之外还有颜色,我想删除单元格所属的整行。

As a final result I want to keep only rows in which the cell in column D has either no fill or white background color.作为最终结果,我只想保留 D 列中的单元格没有填充色或白色背景色的行。

The code below performs that task, but takes so much time.下面的代码执行该任务,但需要很多时间。 The total number of rows that the macro processes is 700.宏处理的总行数为 700。

I provide two different types of code.我提供了两种不同类型的代码。 Both of them take so long.他们两个都需要这么长时间。

CODE 1代码 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

CODE 2代码 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 Nothing Then
            Set DeleteRange = cell
        Else
            Set DeleteRange = Union(DeleteRange, cell)
        End If
    End If
Next cell
If Not DeleteRange Is Nothing Then DeleteRange.EntireRow.Delete

You should use Code 2. Turning off ScreenUpdating and Calculations will speed up the code tremendously.您应该使用代码 2。关闭 ScreenUpdating 和 Calculations 将极大地加速代码。

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

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 Nothing Then
            Set DeleteRange = cell
        Else
            Set DeleteRange = Union(DeleteRange, cell)
        End If
    End If
Next cell
If Not DeleteRange Is Nothing Then DeleteRange.EntireRow.Delete

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

I looked up the Union thing, and adapted your code 1. You can choose to include the screenupdating, and calculation mode here as well, but as deletion only happens at the end of the code it shouldn't make much of a performance difference.我查找了 Union 的内容,并调整了您的代码 1。您也可以选择在此处包含 screenupdating 和计算模式,但由于删除仅发生在代码的末尾,因此不会对性能产生太大影响。

With ws1
    lastrow2 = ws1.Range("A" & Rows.Count).End(xlUp).Row
    For i = lastrow2 To 2 Step -1
    If .Cells(i, "D").Interior.ColorIndex = 2 Or .Cells(i, "D").Interior.ColorIndex = -4142 Then
        Dim DeleteRange as range
        If DeleteRange Is Nothing Then
            Set DeleteRange = .Rows(i).entirerow
        Else
            Set DeleteRange = Union(DeleteRange, .Rows(i).entirerow)
        End If
    End If
    Next i
    DeleteRange.Delete
End With

(Code is untested) (代码未经测试)

Try this code:试试这个代码:

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False

Dim DeleteRange As Range
With ws1
    lastrow2 = .Range("A" & Rows.Count).End(xlUp).Row
    For i = 2 To lastrow2
        If Not .Cells(i, "D").Interior.ColorIndex = -4142 Then
            If Not .Cells(i, "D").Interior.ColorIndex = 2 Then
                If DeleteRange Is Nothing Then
                    Set DeleteRange = .Rows(i)
                Else
                    Set DeleteRange = Union(DeleteRange, .Rows(i))
                End If
            End If
        End If
    Next i
End With

DeleteRange.Delete

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

I nested If s to imitate short-circuiting, which will enhance execution of a code.我嵌套了If来模拟短路,这将增强代码的执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM