セルの範囲を調べて、次のことを実行しようとしています。セルに背景色がある場合はスキップします。背景色がない場合は、行全体を削除します。
現在のコードの何が問題になっていますか?
Sub RemoveRowsThatAreNotHighlighted123()
Dim cell As Range
Dim rng As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set rng = ThisWorkbook.Worksheets("Main").Range("A2:L" & ThisWorkbook.Worksheets("Main").Range("C2").End(xlDown).Row)
For Each cell In rng
If cell.Interior.ColorIndex = 0 Then cell.EntireRow.Delete
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
のxlColorIndexNone
代わりに使用0
:
さらに、行を削除するときは、次のように常に逆方向にループすることをお勧めします。
Sub RemoveRowsThatAreNotHighlighted123()
Dim iRow As Long
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
With ThisWorkbook.Worksheets("Main") ' reference your workbook and worksheet
With .Range("A2:L" & .Range("C2").End(xlDown).Row) ' reference referenced worksheet range from A2 down to column C last not empty cell before first empty one
For iRow = .Rows.Count To 1 Step -1 ' loop from referenced range last row backwards
If .Rows(iRow).Interior.ColorIndex = -4142 Then .Rows(iRow).EntireRow.Delete ' if alll referenced range current row cells have no background then delete row
Next
End With
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加