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

时间:2012-10-07 08:40:30

标签: c# excel range cell

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

如何检查选择(或代码范围表示的单元格)是否在specyfic范围内。例如,如何检查单元格$ P $ 5是否在$ A $ 1范围内:$ Z $ 10

2 个答案:

答案 0 :(得分:5)

使用Application.Intersect,像这样(在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

答案 1 :(得分:0)

根据接受的答案,我添加了 C# 版本(按照问题的要求):

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

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