2016-04-11 124 views
2

我有一個比較兩個範圍的問題。爲了簡單起見,我將採取兩個簡單的範圍M6:M10M6:M8,我想知道,如果第二個是包含在第一個的第一件事情我雖然是寫如何查找範圍是否包含在另一個範圍內? VBA

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

所以也許我不能使用的方法「相交」以這種方式......如何測試範圍的包括任何暗示? 非常感謝!

+0

'Dim range1,range2,inte As range' decla res'range1'和'range2'是變體(不是這就是問題)。另外 - 你提到的錯誤似乎與你顯示的代碼無關,但可能隱藏在方括號中。 –

+0

也相交的方法如果沒有相交,則不返回,可能是一個問題。您應該檢查相交是否爲第一個 –

+0

您能否確認哪條線路會產生錯誤?懷疑它會是'If intersec = range2 Then'。如果您試圖確認intersec和range2完全重疊,請嘗試:'如果intersec.Address = range2.Address Then'。 –

回答

3

這裏有一種方法:

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 
+0

謝謝!!!!!! –

1

我使用它的方式是這樣的:

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

您可以刪除其他或寫沒有什麼,取決於你想要達到的目標。

2

首先,聲明你的範圍1和範圍變量的範圍。

然後,當您將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 
+0

正確..沖和不注意..更新了答案,無需處理任何錯誤。 –

1

這裏的東西,你可以嘗試:

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 
0

另一個額外的變體:

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 
0

你可以做相交的比較確定一個範圍是否包含在另一個範圍內的範圍。一些代碼來顯示這個...

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

0

對於具有多個區域的範圍工作的更穩健的解決方案,不同工作表上的範圍相互之間,範圍包含非常大量的單元格(如.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 
相關問題