gpt4 book ai didi

excel - 如果单元格未突出显示,则删除整行

转载 作者:行者123 更新时间:2023-12-04 11:01:15 24 4
gpt4 key购买 nike

我正在尝试遍历单元格范围并执行以下操作:
如果单元格有背景颜色,则跳过。如果没有背景颜色,则删除整行。

我当前的代码有什么问题?

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

关于excel - 如果单元格未突出显示,则删除整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58780885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com