如何比较两个多区域范围

时间:2015-01-09 14:46:39

标签: excel-vba vba excel

我有两个Range类型的变量,比如r1和r2。如何检查它们是否在同一范围内?如果它是单个单元格或单个连续范围的单元格,则很容易(我们可以假设它们在同一张纸上):

If r1.Address = r2.Address Then

但是如果它们是多区域范围(r1.Areas.Count> 0)呢?在大多数情况下,上面的代码行仍然有效,但区域的地址出现在r1和r2的地址中的顺序可能不同。例如,有些情况下您将拥有以下内容:

Debug.Print r1.Address
Debug.Print r2.Address

可以导致

"$A$1,$B$2"
"$B$2,$A$1"

它们显然是相同的范围,但上述IF语句中的条件将评估为假。

理想情况下我想说IF r1 = r2然后,但这不起作用。也许我可以和联盟一起尝试......

编辑:

我也试过

If r1 Is r2 Then

但这也不起作用。

2 个答案:

答案 0 :(得分:1)

考虑一个简单的双循环:

Sub SameCheck(R1 As Range, R2 As Range)
Dim r As Range

For Each r In R1
    If Intersect(r, R2) Is Nothing Then
        MsgBox "not the same"
        Exit Sub
    End If
Next r

For Each r In R2
    If Intersect(r, R1) Is Nothing Then
        MsgBox "not the same"
        Exit Sub
    End If
Next r

MsgBox "the same"
End Sub

测试如下:

Sub MAIN()
    Call SameCheck(Range("A1:Z100"), Range("B9"))
    Call SameCheck(Range("A1:C3"), Range("A1:C3"))
End Sub

答案 1 :(得分:0)

我在SO的其他地方遇到了一个很好的解决方案:

If (Intersect(r1,r2).Cells.Count = r1.Cells.Count) AND Intersect(r1,r2).Cells.Count = r2.Cells.Count Then

这是有效的,因为如果两个范围不完全相同,那么两个范围的交点将始终具有比至少其中一个范围更少的单元格。

修改

根据Rusan的评论,很明显有些情况下,即使r1和r2是“相同的”,上述条件也会评估为False。我使用引号,因为它需要通过说它们是“相同”的范围来定义我的意思:如果你在r1的外边缘周围画一个边框并且在r2的外边缘周围画一个边框,那么边框必须沿着完全相同的路径。

正如Rusan指出的那样,如果r1和r2中的一个或两个在其中有重叠的单元格,则会出现问题。因此,我们需要首先“去重叠”它们:

Public Function GetNonOverlappedRange(r As Range) As Range

Dim cell As Range, area As Range, newRange As Range

For Each area In r.Areas
    For Each cell In area
        If newRange Is Nothing Then
            Set newRange = cell
        Else
            If Intersect(cell, newRange) Is Nothing Then
                Set newRange = Union(newRange, cell)
            End If
        End If
    Next
Next

Set GetNonOverlappedRange = newRange

End Function

然后

Set r3 = GetNonOverlappedRange(r1)
Set r4 = GetNonOverlappedRange(r2)

If (Intersect(r3,r4).Cells.Count = r3.Cells.Count) AND Intersect(r3,r4).Cells.Count = r4.Cells.Count Then
    'If this is true then r1 and r2 are the "same" range

我应该指出,Gary的学生在下面的回答提供了我的问题的正确答案,我将其标记为正确的答案,但我认为无论如何构建“去重叠”功能是有用的,因为我可以看到许多需要它的情况。