Excel 关于判断两个范围是否有交集,详见附件,请大神们指导

2024-11-23 11:16:23
推荐回答(2个)
回答1:

写了个简单的自定义函数:
Function f$(RngA As Range, RngB As Range)

m = WorksheetFunction.Max(RngA, RngB) '范围内数值最大值m

n = WorksheetFunction.Min(RngA, RngB) '范围内数值最小值n

ReDim a&(n To m) '定义一个范围n To m的数组用于记录

For j = 1 To RngA.Columns.Count

For i = RngA.Cells(1, j) To RngA.Cells(2, j)

a(i) = 1 '首先遍历A区域,循环各个范围内向数组内赋值=1 即标记为该区段已占用

Next

Next

For j = 1 To RngB.Columns.Count

For i = RngB.Cells(1, j) To RngB.Cells(2, j)

If a(i) Then f = "交集 Intersect!": Exit Function

'接着遍历B区域 检查如果对应范围已被占用则提示有交集,退出

Next

Next

f = "无交集 No intersect" '如果全部循环结束都没有交集,则提示无交集

End Function

回答2:

哪来的附件?