失去焦点后检查相交

时间:2015-12-21 14:33:53

标签: excel vba

我一直在尝试检查用户是否刚刚从特定Range个单元格中失去焦点。

Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Static OldRange As Range
    Dim AmountCol As String * 1
    Dim PayedCol As String * 1
    Dim MyRange As Range

    If (Not OldRange Is Nothing) Then
        AmountCol = Chr(Asc(Range("B1").Text) + 1)
        PayedCol = Chr(Asc(Range("B1").Text) + 2)
        Set MyRange = Range(AmountCol & ":" & AmountCol)

        If Not MyRange Is Nothing And Not OldRange Is Nothing Then
            If Not Intersect(OldRange, MyRange) Then
                MsgBox Chr(Asc(Range("B1").Text) + 1) & ":" & Chr(Asc(Range("B1").Text) + 1)
            End If
        End If
    End If

    Set OldRange = Target.Cells(1, 1)
End Sub

有人可以帮我理解这有什么问题吗?该代码在If Not Intersect(OldRange, MyRange) Then

的行中断

1 个答案:

答案 0 :(得分:1)

更改此行:

If Not Intersect(OldRange, MyRange) Then

到此:

If Intersect(OldRange, MyRange) Is Nothing Then