【问题标题】:How to find if a range is included in another range using VBA?如何使用 VBA 查找一个范围是否包含在另一个范围中?
【发布时间】:2016-04-11 11:37:34
【问题描述】:

我在比较两个范围时遇到问题。为简单起见,我将采用两个简单的范围M6:M10M6:M8,我想知道第二个是否包含在第一个中,而我想的第一件事是写

    Sub example()
    Dim range1, range2, inte As range
        Set range1 = range("M6:M10")
        Set range2 = range("M6:M8")
        Set intersec = Intersect(range1, range2)
        If intersec = range2 Then
            [if statement]
        End If
    End Sub

但此过程返回以下错误:

PRB: Error 13 (Type Mismatch) & Error 3061 w/ SQL Queries

所以也许我不能以这种方式使用“相交”方法......关于如何测试范围包含的任何提示? 非常感谢!

【问题讨论】:

  • Dim range1, range2, inte As range 声明 range1range2 是变体(不是这就是问题)另外 - 您提到的错误似乎与您显示的代码无关,但很可能隐藏在方括号中。
  • 还有 intersect 方法 如果不相交,则不返回任何内容,这可能是一个问题。您应该首先检查 intersect 是否为 Nothing
  • 你能确认是哪一行产生了错误吗?怀疑是If intersec = range2 Then。如果您要确认 intersec 和 range2 完全重叠,请尝试:If intersec.Address = range2.Address Then

标签: vba excel range inclusion


【解决方案1】:

这是一种方法:

Sub ProperSubSet()
    Dim range1 As Range, range2 As Range, inte As Range
    Dim r As Range

    Set range1 = Range("M6:M10")
    Set range2 = Range("M6:M8")

    For Each r In range2
        If Intersect(r, range1) Is Nothing Then
            MsgBox "range2 is not a proper subset of range1"
        Exit Sub
        End If
    Next r
    MsgBox "range2 is a proper subset of range1"
End Sub

【讨论】:

    【解决方案2】:

    首先,将 range1 和 range2 变量声明为范围。

    然后在比较 intersec 变量和 range2 变量时,使用 range 方法的 address 属性来比较内容。

    类似:

    Sub example()
    Dim range1 As Range, range2 As Range, intersec As Range
        Set range1 = Range("M6:M10")
        Set range2 = Range("M11:M12")
        Set intersec = Intersect(range1, range2)
        If Not intersec Is Nothing Then
            If intersec.Address = range2.Address Then
                '[CODE GOES HERE]
            End If
        End If
    End Sub
    

    【讨论】:

      【解决方案3】:

      我的使用方式是这样的:

      If Application.Intersect(rng1, rng2) Is Nothing Then 
          'herecomesthecode
      Else
          'herecomesthecode
      End if
      

      您可以删除 else 或写 Not nothing,这取决于您想要实现的目标。

      【讨论】:

        【解决方案4】:

        您可以尝试以下方法:

        Sub Test()
            Dim R1 As Range, R2 As Range, R3 As Range
        
            Set R1 = Application.InputBox("Select first range", , , , , , , 8)
            Set R2 = Application.InputBox("Select second range", , , , , , , 8)
        
            Set R3 = Intersect(R1, R2)
            If Not R3 Is Nothing Then
                If R3.Address = R1.Address Then
                    MsgBox "First Range is subset of second"
                ElseIf R3.Address = R2.Address Then
                    MsgBox "Second Range is subset of first"
                Else
                    MsgBox "Neither range contained in the other"
                End If
            Else
                MsgBox "Ranges are disjoint"
            End If
        
        End Sub
        

        【讨论】:

          【解决方案5】:

          另一个附加变体:

          Sub ProperSubSet2()
              Dim range1 As Range, range2 As Range
              Set range1 = [M6:M10]
              Set range2 = [M6:M8]
              Set rComp = Intersect(range2, range1)
          
              If Not rComp Is Nothing Then
                  If rComp.Cells.Count = range2.Cells.Count Then
                      MsgBox "range2 is a proper subset of range1"
                  Else
                      MsgBox "range2 is not a proper subset of range1"
                  End If
              Else
                  MsgBox "Both ranges aren't intersected at all!"
              End If
          
          End Sub
          

          【讨论】:

            【解决方案6】:

            您可以将相交与范围进行比较,以确定一个范围是否包含在另一个范围内。一些代码来显示这个......

            Sub TestExample()
                Dim Range1 As Range: Set Range1 = Range("M6:M10")
                Dim Range2 As Range: Set Range2 = Range("M6:M8")
                MsgBox Example(Range1, Range2)
            End Sub
            
            Function Example(Range1 As Range, Range2 As Range) As Integer
                Dim Overlay As Range: Set Overlay = Application.Intersect(Range1, Range2)
                If Not Overlay Is Nothing Then
                    If Overlay.Address = Range1.Address Then Example = Example + 1
                    If Overlay.Address = Range2.Address Then Example = Example + 2
                End If
            End Function
            

            如果没有范围完全包含在另一个范围内,该函数将返回 0,如果第一个范围包含在第二个范围内,则返回 1,如果第二个范围包含在第一个范围内,则返回 2,如果范围相等,则返回 3

            【讨论】:

              【解决方案7】:

              对于一个更强大的解决方案,它适用于具有多个区域的范围、不同工作表上的范围、具有大量单元格的范围(所以 .CountLarge,而不是 .Count),那么这将起作用:

              Function RangeContainsRange(BigRange As Range, SmallRange As Range) As Boolean
                  If BigRange.Parent Is SmallRange.Parent Then
                      RangeContainsRange = Application.Union(BigRange, SmallRange).Cells.CountLarge = BigRange.Cells.CountLarge
                  Else
                      RangeContainsRange = False
                  End If
              End Function
              

              【讨论】:

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