如何保持初始单元格颜色,而代码是高亮度的活动行 [英] how to keep initial cell color, while code is hilighting active row
问题描述
我有这个工作簿代码:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
'toggles worksheet colors
'code will remove all color
'and color active row and column
If ActiveCell.Interior.ColorIndex <> xlNone Then
Cells.Interior.ColorIndex = xlNone
Else
Cells.Interior.ColorIndex = xlNone
ActiveCell.EntireRow.Interior.ColorIndex = 4
End If
End Sub
并且效果很好。但如果一行具有初始颜色,则会被删除。让我知道
活动行将如何突出显示,并通过更改行,将获得其初始颜色?
and works good. but if a row has an initial color, it will be removed. let me know how active row will be highlighted and by changing the row, will get its initial color?
推荐答案
这是一个替代方法,它利用Excel总是覆盖事实已经有条件格式化
Here's an alternate approach which makes use of the fact that Excel always "overlays" Conditional Formatting on top of whatever formatting is already on the sheet.
定义工作表级别名称ROWNUM并分配值0。
Define a worksheet-level name "ROWNUM" and assign a value of 0.
使用公式 =(ROW()= ROWNUM)
添加条件格式,并添加要用于行突出显示的任何格式。
Add a conditional format using the formula =(ROW()=ROWNUM)
and add whatever formatting you want to use for row highlighting.
您的SelectionChange子然后只是:
Your SelectionChange sub is then just:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Me.Names("ROWNUM").RefersToR1C1 = "=" & Target.Cells(1).Row
End Sub
这篇关于如何保持初始单元格颜色,而代码是高亮度的活动行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!