我目前正在处理一个工作表,在该工作表中,我希望能够通过双击感兴趣的单元格来更改特定范围的单元格的颜色。我只希望将其应用于整个工作表的3个单元格,其余的不具备此功能。我在网上找到了以下代码,但是,它不是特定于范围的,并且适用于整个工作表。我有办法吗?
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Select Case Target.Interior.ColorIndex
Case xlNone, 4: Target.Interior.ColorIndex = 3
Case 3: Target.Interior.ColorIndex = 45
Case 45: Target.Interior.ColorIndex = 4
Case Else: Target.Interior.ColorIndex = xlNone
End Select
End Sub
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.ColorIndex = xlNone
End Sub
答案 0 :(得分:2)
使用Intersect
检查Target
是否在C46:C48
范围内。
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
If Not Intersect(Target, Me.Range("C46:C48")) Is Nothing Then
Select Case Target.Interior.ColorIndex
Case xlNone, 4: Target.Interior.ColorIndex = 3
Case 3: Target.Interior.ColorIndex = 45
Case 45: Target.Interior.ColorIndex = 4
Case Else: Target.Interior.ColorIndex = xlNone
End Select
End If
End Sub