Intersection of two arrays of ranges

后端 未结 1 1991
再見小時候
再見小時候 2021-01-19 16:39

I currently have two arrays each of which contain ranges. How would you go about getting the intersection of these two arrays. In other words, I would like to get an array o

相关标签:
1条回答
  • 2021-01-19 17:42

    you can use this code. The idea is to merge your array in a single range using an iterative Union. Then you can use the built-in Intersect.

    Function IntersectArray(array1() As Range, array2() As Range) As Range
        Dim unionRangeArray1 As Range, unionRangeArray2 As Range
        Dim i As Integer
    
        Dim lbound1 As Integer: lbound1 = LBound(array1)
        Dim lbound2 As Integer: lbound2 = LBound(array2)
    
        Set unionRangeArray1 = array1(lbound1)
        Set unionRangeArray2 = array2(lbound2)
    
        For i = lbound1 + 1 To UBound(array1)
            Set unionRangeArray1 = Application.Union(unionRangeArray1, array1(i))
        Next
    
        For i = lbound2 + 1 To UBound(array2)
            Set unionRangeArray2 = Application.Union(unionRangeArray2, array2(i))
        Next
    
        Set IntersectArray = Application.Intersect(unionRangeArray1, unionRangeArray2)
    End Function
    
    0 讨论(0)
提交回复
热议问题