PDA

View Full Version : Confusion about Union method



fboehlandt
10-07-2008, 08:54 AM
Hi everyone,
I would like to create a new joint range from two discontinuous ranges on the same worksheet. So far I have the following:


Sub Test2()
Dim wb As Workbook
Dim ws1, ws2 As Worksheet
Dim rng1a, rng1b, rng_union As Range
Set wb = ThisWorkbook
Set ws1 = wb.Worksheets("testsheet_roi2")
Set ws2 = wb.Worksheets("result")
ws1.Select
Set rng1 = Range(Cells(2, 2), Cells(32, 2))
Set rng2 = Range(Cells(2, 4), Cells(32, 4))

Set rng_union = Union(rng1, rng2)

ws2.Select
Range(Cells(2, 2), Cells(63, 2)).Value = rng_union.Value

End Sub

this only display rng1 instead of both ranges. What am I doing wrong?

fboehlandt
10-07-2008, 09:20 AM
Note that the two ranges are not adjacent, this seems to be one of the root causes (the code works if they are)

Paul_Hossler
10-07-2008, 12:16 PM
It's one range with 2 areas

not tested,


MsgBox rng_union.areas(1).address
MsgBox rng_union.areas(2).address


Paul