[英]Delete entire row, if a cell has a background color
我正在嘗試刪除D列中的單元格具有背景色的所有行。 我已經在下面編寫了代碼,但是每次運行它時,它都會無限期運行,直到最終崩潰。 ws1
和lastrow2
都定義良好(我提到它是為了清除這種可能導致我的宏未運行的可能性)
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
不要使用0 :
Sub qwerty()
Dim ws1 As Worksheet: Set ws1 = ActiveSheet
Dim Nodel As Boolean
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 = -4142 Then
Nodel = True
End If
If Not Nodel Then
.Rows(i).EntireRow.Delete
End If
Next i
End With
End Sub
編輯#1:
如果要保留具有白色背景的單元格,請首先驗證“白色”對應於“ colorindex = 2”,然后在代碼中使用2代替-4142 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.