1

我有一个小宏,可以在根据月份进行编辑时突出显示单元格。我想让这个子程序只发生在 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
4

1 回答 1

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。

于 2013-05-15T18:15:11.937 回答