Checking if selected cell is in specific range

2019-07-04 17:44发布

问题:

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

回答1:

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