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. 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.
The code below performs that task, but takes so much time. The total number of rows that the macro processes is 700.
I provide two different types of code. Both of them take so long.
CODE 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
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
If
statements in code one, by usingOR
logic – Luuklag