Home > Software engineering >  Stop firing event SelectionChange (already Intersect used) if it is intersect with another range?
Stop firing event SelectionChange (already Intersect used) if it is intersect with another range?

Time:12-23

I am using below code to run a macro Calendar_Advanced if any cell selected in column M.
the Problem: If I selected any cell in the same time with Range M:M , the event fire also, Like if I select all row.

Private Sub worksheet_SelectionChange(ByVal Target As Excel.Range)

  Dim LastRow As Long: LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
      
  If Not Intersect(Target, Range("M3:M" & LastRow)) Is Nothing Then
        Call Calendar_Advanced
    End If
  End Sub

I tried to add to if condition And Selection.Cells.Count = 1 it works,But prevent multi selection (Calendar_Advanced) to run on column M.
as always,any help will be appreciated

CodePudding user response:

Count the columns to make sure it is just M.

Private Sub worksheet_SelectionChange(ByVal Target As Excel.Range)

    Dim LastRow As Long: LastRow = ActiveSheet.Cells(Rows.Count, "B").End(xlUp).Row
    If Not Intersect(Target, Range("M3:M" & LastRow)) Is Nothing Then
        If Target.Columns.Count = 1 Then
            Calendar_Advanced
        End If
    End If
End Sub
  • Related