Union of ranges

NerdyGirl

New Member
Joined
May 13, 2016
Messages
8
Hi, I'm attempting to define a range based on the union of two ranges.

Code:

Dim rUnion As Range, r1 As Range, r2 As Range

Set r1 = Range("GJ7:GP7")
Set r2 = Range("GS7")
Set rUnion = Union(r1, r2)

Debug.Print rUnion(1).Address
Debug.Print rUnion(2).Address
Debug.Print rUnion(3).Address
Debug.Print rUnion(4).Address
Debug.Print rUnion(5).Address
Debug.Print rUnion(6).Address
Debug.Print rUnion(7).Address
Debug.Print rUnion(8).Address
Debug.Print r2.Address

The above rUnion addresses print correctly until rUnion(8) which returns $GJ$8. The r2.Address correctly prints $GS$7. I cannot figure out why/how the union references GJ8. Thoughts? :confused:
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
The ranges are treated as two separate ranges, accessible via Range.Areas property.

Try something like this

Code:
Sub Test()
    Dim rUnion As Range, r1 As Range, r2 As Range
    
    Set r1 = Range("GJ7:GP7")
    Set r2 = Range("GS7")
    Set rUnion = Union(r1, r2)
    
    Debug.Print rUnion.Areas(1)(1).Address
    Debug.Print rUnion.Areas(1)(2).Address
    Debug.Print rUnion.Areas(1)(3).Address
    Debug.Print rUnion.Areas(1)(4).Address
    Debug.Print rUnion.Areas(1)(5).Address
    Debug.Print rUnion.Areas(1)(6).Address
    Debug.Print rUnion.Areas(1)(7).Address
    Debug.Print rUnion.Areas(2)(1).Address
    Debug.Print r2.Address


End Sub

Hope this helps

M.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top