繁体   English   中英

根据单元格颜色删除行Excel VBA

[英]Delete rows based on cell color excel vba

我正在尝试根据单元格颜色删除行。 我遇到的问题是我编写的代码不断“跳过行”:

 For i = 2 To lastRow
'MsgBox (Cells(i, 1) & " " & Cells(i, 1).Interior.ColorIndex)
    If Worksheets("Export Worksheet").Cells(i, 1).Interior.ColorIndex <> "4" Then
       'MsgBox ("Row " & i & " will be deleted")
       Rows(i).EntireRow.Delete
       'MsgBox ("i is currently " & i)
       i = i + 1
    'MsgBox ("i is now " & i)
    End If

Next i

有人可以帮忙吗? 谢谢!!

删除行时应向后循环。

 For i = lastRow to 2 Step -1 'Step --1 tells VBA to loop in reverse
'MsgBox (Cells(i, 1) & " " & Cells(i, 1).Interior.ColorIndex)
    If Worksheets("Export Worksheet").Cells(i, 1).Interior.ColorIndex <> "4" Then
       'MsgBox ("Row " & i & " will be deleted")
       Rows(i).EntireRow.Delete
       'MsgBox ("i is currently " & i)
       i = i + 1
    'MsgBox ("i is now " & i)
    End If

Next i

暂无
暂无

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

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