select单元格时出现excel运行vba代码的问题

我已经写了下面的函数,但是我得到一个错误,由于If intersect …行上的时间不匹配,导致无法编译。 当我将鼠标hover在“目标”而不是范围上时,debugging显示所选单元格的值(我不知道这是否表示问题)

Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim lastEntry As Integer Dim shiftEntries As Range lastEntry = LastEntryRow() Set shiftEntries = Range("A11:L" & lastEntry) If Intersect(Target, shiftEntries) Then Dim shiftDate As String shiftDate = Cells(Target.Row, 1).Value Cells(10, 15) = ShiftsInSevenDays(shiftDate) End If End Sub 

任何帮助深表感谢。

请尝试以下修正的代码:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim lastEntry As Integer Dim shiftEntries As Range lastEntry = LastEntryRow() Set shiftEntries = Range("A11:L" & lastEntry) If Not Intersect(Target, shiftEntries) Is Empty Then Dim shiftDate As String shiftDate = Cells(Target.Row, 1).Value Cells(10, 15) = ShiftsInSevenDays(shiftDate) End If End Sub 

相交将返回一个不是布尔值(真或假)的范围。 我不确定你是否应该使用Is EmptyIs Nothing ,但是它是其中之一。 问候,