当选择单元格时,用指定的颜色高亮显示选定区域左上角单元格整行和整列。
作者:小智雅汇
代码1
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
Dim highLightRng As Range
Dim rng1 As Range
Dim rng2 As Range
Cells.Interior.ColorIndex = xlNone
Set rng1 = Rows(ActiveCell.Row)
Set rng2 = Columns(ActiveCell.Column)
On Error Resume Next
Set highLightRng = Application.Union(rng1, rng2)
highLightRng.Interior.ThemeColor = xlThemeColorAccent3
highLightRng.Interior.TintAndShade = 0.799981688894314
Set rng1 = Nothing
Set rng2 = Nothing
Set highLightRng = Nothing
' ActiveCell.Value = ActiveCell.Address
Application.ScreenUpdating = True
End Sub
代码2
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
Cells.Interior.ColorIndex = -4142
Columns(Target.Column).Interior.ColorIndex = 20
Rows(Target.Row).Interior.ColorIndex = 20
Application.ScreenUpdating = True
End Sub
以上两种代码均可得到结果
代码描述
利用Worksheet_SelectionChange事件,在工作表的单元格选取发生改变时运行指定的程序。
用Union方法得到ActiveCell对象的整行和整行;
将Union得到的对象通过.Interior.TintAndShade设置颜色。
过程运行效果