2017-03-15 206 views
2

我有兩個問題: 1)如何檢查交集或範圍是否不爲空?例如,如果我想檢查它是否空我寫例如VBA for Excel - 如何檢查範圍的交集不爲空

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

把不是如果後:'如果不是application.intersect(R1,R2)是nothing' –

回答

1

要查看是否交集爲空可以使用

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

要查看是否3米範圍相交一起是更嚴厲的。這裏的邏輯

如果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

要查看是否兩個範圍的交集的第三範圍:

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