Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading