选择单元格时突出显示整行但不丢失原始行的原始格式?

时间:2012-09-05 16:47:34

标签: excel-vba excel-2010 vba excel

问题是,当我突出显示某行颜色的行时,行的原始颜色消失了,所以我尝试了这段代码,再次,它删除了行的原始格式和颜色

这是来自http://www.mcgimpsey.com/excel/highlightrow.html

的代码
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
        Const cnNUMCOLS As Long = 256
        Const cnHIGHLIGHTCOLOR As Long = 36  'default lt. yellow
        Static rOld As Range
        Static nColorIndices(1 To cnNUMCOLS) As Long
        Dim i As Long
        If Not rOld Is Nothing Then 'Restore color indices
            With rOld.Cells
                If .Row = ActiveCell.Row Then Exit Sub 'same row, don't restore
                For i = 1 To cnNUMCOLS
                    .Item(i).Interior.ColorIndex = nColorIndices(i)
                   Next i
            End With
        End If
        Set rOld = Cells(ActiveCell.Row, 1).Resize(1, cnNUMCOLS)
        With rOld
            For i = 1 To cnNUMCOLS
                nColorIndices(i) = .Item(i).Interior.ColorIndex
            Next i
            .Interior.ColorIndex = cnHIGHLIGHTCOLOR
        End With
    End Sub

那么,我该怎么处理这个问题呢?如何保留范围的历史颜色,以便在取消选择该行时它会正确恢复?

1 个答案:

答案 0 :(得分:1)

使用条件格式伪造它。

使用条件格式设置将单元格设置为所需的颜色。作为条件,请使用公式=ROW()=$C$12

然后(在此示例中)将C12更改为您要突出显示的行。将其设置为范围之外的行以删除突出显示。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
ActiveSheet.Cells(12, 3) = Target.Row
End Sub
相关问题