简体   繁体   中英

How to clear content of rows except column A?

I have a macro which removes the content of the entire row with cells in light yellow in my selection (in my macro Range("B1:B3000")).

在此处输入图片说明

My macro works but I would like to not remove the content of column A.

I guess I should modify the line: rColored.EntireRow.ClearContents

Sub SelectColoredCellsVariantremovecelllightyellow()
    Dim rCell As Range
    Dim lColor As Long
    Dim rColored As Range
    Dim myselection As Range
    Set myselection = Range("B1:B3000")
    lColor = 10092543
    Set rColored = Nothing
    For Each rCell In myselection
        If rCell.Interior.Color = lColor Then
            If rColored Is Nothing Then
                Set rColored = rCell
            Else
                Set rColored = Union(rColored, rCell)
            End If
        End If
    Next
    rColored.EntireRow.ClearContents
    Set rCell = Nothing
    Set rColored = Nothing
End Sub

Here you can use Intersect to restrict the range to be cleared:

Replace

rColored.EntireRow.ClearContents

with

If Not rColored Is Nothing Then
    Intersect(rColored.EntireRow, Columns("B:AB")).ClearContents
End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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