user1658223
user1658223

Reputation: 725

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

Upvotes: 4

Views: 3606

Answers (2)

EylM
EylM

Reputation: 6103

Based on the accepted answer, I am adding the C# version (as requested in the question):

var myRange = Application.Range["$P$5"];
var testRange = Application.Range["$A$1:$Z$10"];

if (Application.Intersect(myRange, testRange) != null)
{
    // do something 
    
}

Upvotes: 0

chris neilsen
chris neilsen

Reputation: 53126

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

Upvotes: 5

Related Questions