检查所选单元格是否在特定范围内

问题描述:

我正在使用C#创建Excel加载项。

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

如何检查selected(或由代码范围表示的单元格)是否在特定范围内。例如如何检查单元格$ P $ 5是否在$ A $ 1:$ Z $ 10

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

使用 Application.Intersect ,像这样(在VBA中)

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