2017-03-15 6 views
2

私は2つの質問があります: 1)交差点または範囲が空でないかどうかを確認するにはどうすればよいですか?例えば、私が空であるかどうかをチェックしたい場合は、ExcelのVBA - 範囲の交差をチェックする方法は空ではありません

if application.intersect(r1,r2) is nothing 

がありますが、何も否定されているものはありますか?何も例のようには機能しませんでした。

2)範囲を計算するにはどうすればよいですか?たとえば、範囲r1、r2、r3があり、r1とr2の交点がr3であるかどうかをチェックしたいとします。私が試してみましたし、うまくいきませんでした二つの事:

1 - application.intersect(r1,r2) = r3 
2 - application.intersect(r1,r2) is r3 

は私が得ることができる任意のヘルプをお願い申し上げます、ありがとう!

+2

がない場合の後に置く: '(R1、R2)をapplication.intersectない場合nothing'あります –

答えて

1

交差点は、あなたが3つの範囲は厳しいです一緒に交差するかどうかを確認するには

If Not Application.WorksheetFunction.CountA(Application.Intersect(rng1, rng2)) > 0 Then 
    MsgBox "You Intersection is Empty!" 
    Exit Sub 
End If 

を使用することができます空であるかどうかを確認します。論理は次のとおりです

aとbとcが何かを行うよりも交差する場合

Set isect = Application.Intersect(Range("rg1"), Range("rg2")) 
Set fsect = Application.Intersect(Range("rg2"), Range("rg3")) 
Set gsect = Application.Intersect(Range("rg1"), Range("rg3")) 
if isect = True and fsect = Tru and gsect = True then 
    ' They all intersect each other 
    ' Put your code here 
end if 
4

二つの範囲の交点が第3の範囲であるかどうかを確認するために:

Set intRng = Intersect(R1, R2) 
If Not intRng Is Nothing then 
    Set intRng = Intersect(intRng, R3) 
    If Not Intersect(intRng) Is Nothing 
     If intRng.Address = R3.Address Then MsgBox "intersection of ranges R1 and R2 is range R3" 
    End If 
End If