突出显示当前行而不删除现有单元格颜色 [英] Highlight Current Row Without Deleting Existing Cell Colours

查看:92
本文介绍了突出显示当前行而不删除现有单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面找到了代码,虽然它突出显示了整个行,​​但它也删除了以前有色单元格中的颜色.

I found the code below, and while it highlights the entire row it also removes the color from any previously colored cell.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    Application.ScreenUpdating = False
    ' Clear the color of all the cells
    Target.Parent.Cells.Interior.ColorIndex = 0
    With Target
        ' Highlight the entire row and column that contain the active cell
        .EntireRow.Interior.ColorIndex = 8
    End With
    Application.ScreenUpdating = True

End Sub

我想在选择一个单元格(可能已经有颜色)时突出显示整个行,但是当我移至另一行中的单元格时,先前突出显示的行应返回其先前的颜色.

I would like to highlight the entire row on selection of a cell (that may already be colored), but when I move to a cell in a different row, the previously highlighted row should return to its previous color.

是否可以修改先前选择的单元格/行?

Is there a way to modify the previously selected cells/rows?

推荐答案

条件格式会覆盖常规"格式(无需替换),因此,如果您尚未应用CF,这是一种方便的方式来高亮显示行而无需更改任何现有的单元格颜色.

Conditional formatting overrides "regular" formatting (without replacing it), so if you don't already have some CF applied it's a convenient way to highlight a row without zapping any existing cell colors.

这是一个非常基本的示例:

Here's a very basic example:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Cells.Count > 1 Then Exit Sub

    Application.ScreenUpdating = False

    Me.Cells.FormatConditions.Delete

    With Target.EntireRow.FormatConditions.Add(Type:=xlExpression, _
                                               Formula1:="=TRUE") 
        .SetFirstPriority
        .Interior.Color = 65535
    End With

    Application.ScreenUpdating = True

End Sub

这篇关于突出显示当前行而不删除现有单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆