一个简单的解决方案,当选择更改时放置单元格颜色
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Selection.Interior.ColorIndex = xlColorIndexNone
Selection.Interior.Color = RGB(204, 204, 204)
End Sub
一种复杂的解决方案,仅在失去焦点时才更改单元格颜色
在标准模块中:
Option Explicit
Public s As Range
要在工作表中使用它:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set s = Selection
End Sub
在ThisWorkbook
:
Private Sub Workbook_Deactivate()
If s Is Nothing Then
Set s = Selection
Exit Sub
End If
s.Interior.ColorIndex = xlColorIndexNone
s.Interior.Color = RGB(204, 204, 204)
' This is optional formatting to make the cells look more like they're actually selected
s.Borders.Color = RGB(130, 130, 130)
s.BorderAround _
Color:=RGB(30, 130, 37), Weight:=xlThick
End Sub
Private Sub Workbook_Activate()
If s Is Nothing Then
Set s = Selection
Exit Sub
End If
s.Interior.ColorIndex = xlColorIndexNone
s.Borders.ColorIndex = xlColorIndexNone
End Sub
引用:简单的解决方案是基于脱答案由@戴夫 ; 复杂的解决方案是从许多来源汇集在一起的,尤其是在本帖子中@JohnColeman的帮助下。