5

2 つの範囲の比較に問題があります。簡単にするために、2 つの単純な範囲M6:M10とを取り上げM6: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

したがって、この方法で「交差」する方法を使用できない可能性があります...範囲の包含をテストする方法に関するヒントはありますか? どうもありがとうございました!

4

7 に答える 7

6

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
于 2016-04-11T11:50:34.660 に答える
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
于 2016-04-11T11:56:42.763 に答える
1

私がそれを使用している方法は次のようなものです:

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

達成したい内容に応じて、else を削除するか、Not Nothing を記述できます。

于 2016-04-11T11:49:21.283 に答える
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 を返し、最初の範囲が 2 番目の範囲に含まれている場合は 1 を返し、2 番目の範囲が最初の範囲に含まれている場合は 2 を返し、範囲が等しい場合は 3 を返します。

于 2016-04-12T23:39:30.793 に答える
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
于 2016-04-11T13:30:22.637 に答える