如果文本与特定颜色匹配如何删除行

问题描述 投票:0回答:1

如果文本是绿色,我需要删除整行。我编写了一个代码,但最终会删除整个工作表,如果我将代码中的范围更改为某个范围,则该代码一次只会删除我需要的一半。如何将范围称为整个工作表以及如何解决一次删除一半的问题?

这是迄今为止我的代码:

Sub DeleteHighlights()
 
 Dim rng1 As Range

 Set rng1 = ThisWorkbook.Worksheets("Sheet1").Range("A1").CurrentRegion
 Dim arr1: arr1 = rng1.Value
Range("rng1").Select
For Each cell In Selection
    If cell.Font.Color = vbGreen Then
        cell.delete
    End If
Next cell
End Sub
excel vba delete-row
1个回答
0
投票
Option Explicit

Sub DeleteHighlights()
   
    Dim r As Range, c As Range, colRow As Collection, i As Long
    Set colRow = New Collection
    For Each r In ThisWorkbook.Worksheets("Sheet1").Range("A1").CurrentRegion.Rows
        For Each c In r.Columns
            If c.Font.Color = vbGreen Then
                colRow.Add r, CStr(r.Row)
                Exit For
            End If
        Next
    Next
    ' delete rows scanning up
    For i = colRow.Count To 1 Step -1
       colRow(i).Delete
    Next
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.