Pat*_*rez 29
来自帮助:
Set isect = Application.Intersect(Range("rg1"), Range("rg2"))
If isect Is Nothing Then
MsgBox "Ranges do not intersect"
Else
isect.Select
End If
Run Code Online (Sandbox Code Playgroud)
san*_*ica 13
如果要测试的两个范围(您的给定单元格和给定范围)不相同Worksheet,则会Application.Intersect抛出错误.因此,一种避免它的方法就是这样
Sub test_inters(rng1 As Range, rng2 As Range)
If (rng1.Parent.Name = rng2.Parent.Name) Then
Dim ints As Range
Set ints = Application.Intersect(rng1, rng2)
If (Not (ints Is Nothing)) Then
' Do your job
End If
End If
End Sub
Run Code Online (Sandbox Code Playgroud)
使用Microsoft Excel中的VBA确定单元格是否在范围内:
从链接的站点(保持信用到原始提交者):
由Erlandsen Data Consulting 提供的VBA宏提示提供Microsoft Excel应用程序开发,模板定制,支持和培训解决方案
Function InRange(Range1 As Range, Range2 As Range) As Boolean
' returns True if Range1 is within Range2
InRange = Not (Application.Intersect(Range1, Range2) Is Nothing)
End Function
Sub TestInRange()
If InRange(ActiveCell, Range("A1:D100")) Then
' code to handle that the active cell is within the right range
MsgBox "Active Cell In Range!"
Else
' code to handle that the active cell is not within the right range
MsgBox "Active Cell NOT In Range!"
End If
End Sub
Run Code Online (Sandbox Code Playgroud)