Search code examples
c#excelrangecell

Checking if selected cell is in specific range


I'm using C# to create a Excel Add-In.

How can I check if selected(or cell represented by a range in code) is in specyfic range. For example how to check if cell $P$5 is in range $A$1:$Z$10


Solution

  • Use Application.Intersect, like this (in VBA)

    Sub TestIntersect()
        Dim MyRange As Range
        Dim TestRange As Range
    
        Set TestRange = [$A$1:$Z$10]
        Set MyRange = [P5]
    
        If Not Application.Intersect(MyRange, TestRange) Is Nothing Then
            Debug.Print "the ranges intersect"
        End If
    
    End Sub