选择单元格时获取 excel 到 运行 vba 代码的问题

Problems getting excel to run vba code when a cell is selected

我已经编写了下面的函数,但我收到一个错误,由于 If intersect... 行上的时间不匹配,无法编译。当我将鼠标悬停在 'Target' 而不是范围时,调试显示所选单元格的值(我不知道这是否表明问题所在)

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

相交将 return 范围不是布尔值(TRUE 或 FALSE)。我不确定您应该使用 Is Empty 还是 Is Nothing,但它是两者之一。此致,