简体   繁体   English

如何使用Union合并两个非相邻范围?

[英]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. 我想创建一个结合了下表第一行和第三行的范围(Rng3)。

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. 现在,当我检查Rng3时,我看到只有数据的第一行在此范围(1,2,3,4,5),而不是表的第三行。

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 . Rng3定义为Range ,将其他定义为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 : 据我所知,当您使用UNION它会组合列中的范围而不是行中的范围,您可以尝试在使用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). 您是对的,实际上我一直在尝试不同的替代方案而没有成功,我认为VBA做得对,我的意思是,当您在非连续范围内使用UNION时,您确实会获得一个新范围,但这不再是“矩阵类”了,因此您不能轻松地对其进行迭代,并且此新范围是无效的,因为它是一列范围(VBA堆叠拖曳范围而不是合并它们)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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