我有一个小宏,根据它的月份来突出显示细胞。我想让这个子程序只发生在D列。有没有办法做到这一点?代码如下:
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Month(Date)
Case Is = 1
Range(curCell).Interior.ColorIndex = 20
Case Is = 2
Range(curCell).Interior.ColorIndex = 24
Case Is = 3
Range(curCell).Interior.ColorIndex = 33
Case Is = 4
Range(curCell).Interior.ColorIndex = 18
Case Is = 5
Range(curCell).Interior.ColorIndex = 23
Case Is = 6
Range(curCell).Interior.ColorIndex = 45
Case Is = 7
Range(curCell).Interior.ColorIndex = 22
Case Is = 8
Range(curCell).Interior.ColorIndex = 38
Case Is = 9
Range(curCell).Interior.ColorIndex = 35
Case Is = 10
Range(curCell).Interior.ColorIndex = 31
Case Is = 11
Range(curCell).Interior.ColorIndex = 44
Case Is = 12
Range(curCell).Interior.ColorIndex = 48
End Select
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
curCell = ActiveCell.Address(Columns(0, 0))
End Sub
答案 0 :(得分:1)
你可以试试这个:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range
Set r = Application.Intersect(Target, Me.Columns("D"))
If Not r Is Nothing Then
' proceed with r here instead of Target
' ...
End If
End Sub
实际上,你似乎还没有使用Target。它为您提供了发生更改的范围对象。你不需要那个curCell。