2017-03-15 53 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