简体   繁体   中英

How to combine two non-adjacent ranges using Union?

I want to create a range (Rng3) that combines the first row and the third row of the table below.

1   2   3   4   5
11  22  33  44  55
111 222 333 444 555

To do this, I use this script:

Sub combineRange()

Dim Rng1, Rng2, Rng3 As Range

Set Rng1 = ActiveSheet.Range("A1:E1")
Set Rng2 = ActiveSheet.Range("A3:E3")

Set Rng3 = Union(Rng1, Rng2)

End Sub

Now, when I inspect Rng3 I see that only the first row of the data is in this range (1,2,3,4,5) and not the third row of the table.

What am I doing wrong?

If the ranges are adjacent, then it does work!

Sub combineRange()

Dim Rng1, Rng2, Rng3 As Range

Set Rng1 = ActiveSheet.Range("A1:E1")
Set Rng2 = ActiveSheet.Range("A2:E2")

Set Rng3 = Union(Rng1, Rng2)

End Sub

Check again your code. What do you get here?

Sub combineRange()

    Dim Rng1 As Range, Rng2 As Range, Rng3 As Range

    Set Rng1 = ActiveSheet.Range("A1:E1")
    Set Rng2 = ActiveSheet.Range("A3:E3")

    Set Rng3 = Union(Rng1, Rng2)
    MsgBox (Rng3.Address)

End Sub

Note: the line

Dim Rng1, Rng2, Rng3 As Range

defines Rng3 as a Range and the others as Variant . Use:

Dim Rng1 As Range, Rng2 As Range, Rng3 As Range

As far as I know when you use UNION it combines ranges in columns not rows, you could try transposing your ranges befores using UNION :

Sub combineRange()

    Dim Rng1,Rng2,Rng3,Rng4 as Range

    Set Rng1 = application.transpose(ActiveSheet.Range("A1:E1"))
    Set Rng2 = application.transpose(ActiveSheet.Range("A3:E3"))

    Set Rng3 = Union(Rng1, Rng2)
        Rng4=application.transpose(Rng3)



End Sub

You are right actually I have been triying different alternatives without success, I think that VBA is doing it right i mean, when you use UNION in non contiguous ranges you indeed get a new range but this is not "kind of matrix anymore" so you cannot iterate throug it with ease and this new range is useless since it is a one column range (VBA Stacks the tow ranges instead of merge them).

Please Check this example and I hope it helps.

Sub test()
 Dim R1, R2, R3, R4 As Range


  Set R1 = ThisWorkbook.Sheets(2).Range("A2:A10")
  Set R2 = ThisWorkbook.Sheets(2).Range("B2:B10")
  Set R3 = ThisWorkbook.Sheets(2).Range("A2:C10")
 Set R4 = Application.Union(Application.Index(R3, , 1), Application.Index(R3, , 3))
 Set R5 = Application.Intersect(R4, R4)

 Debug.Print R4.Address
 Debug.Print R4.Rows.Count
 Debug.Print R4.Columns.Count

 For Each mycell In R4
  Debug.Print mycell
 Next

在此处输入图片说明

Succeeded by defining the range as a collection and by using a function that transforms the collection to an array. See code below:

Sub combineRange()

Dim CombinedRange As Collection
Set CombinedRange = New Collection

CombinedRange.Add ActiveSheet.Range("A1:E1")
CombinedRange.Add ActiveSheet.Range("A3:E3")

'transfer cominedRange to array using function CollectionToArray
varTable = CollectionToArray(CombinedRange)

End Sub

 Function CollectionToArray(col As Collection) As Variant()
    Dim arr() As Variant, index As Long, it As Variant
    ReDim arr(col.Count - 1) As Variant
    For Each it In col
        arr(index) = it
        index = index + 1
    Next it
    CollectionToArray = arr
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM