【问题标题】:Checking if selected cell is in specific range检查所选单元格是否在特定范围内
【发布时间】:2012-09-27 20:29:00
【问题描述】:

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

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

范围内

【问题讨论】:

    标签: c# excel range cell


    【解决方案1】:

    使用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
    

    【讨论】:

      【解决方案2】:

      根据接受的答案,我正在添加 C# 版本(根据问题中的要求):

      var myRange = Application.Range["$P$5"];
      var testRange = Application.Range["$A$1:$Z$10"];
      
      if (Application.Intersect(myRange, testRange) != null)
      {
          // do something 
          
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-08
        相关资源
        最近更新 更多