VBA Union – Join Multiple Ranges

Associated Files Download Links

Union – Join Multiple Ranges

You might want to combine or join multiple ranges if the range string is too long, or you want to attempt to make your code easier to work with. The need to combine multiple range strings is probably most common when selecting a large number of non contiguous cells with VBA. One way to join multiple range strings is using VBA’s Union function. The syntax for the Union function is:

1

Union(Arg1 asRange,Arg2 asRange,etc,etc)asRange

The following is an example of using Union. First you declare some range variables, then set the range variables equal to something, and finally combine them using Union.

1

2

3

4

5

6

7

8

9

10

11

SubUnionExample()

DimRng1,Rng2,Rng3 AsRange

SetRng1=Range("A1,A3,A5,A7,A9,A11,A13,A15,A17,A19,A21")

SetRng2=Range("C1,C3,C5,C7,C9,C11,C13,C15,C17,C19,C21")

SetRng3=Range("E1,E3,E5,E7,E9,E11,E13,E15,E17,E19,E21")

Union(Rng1,Rng2,Rng3).Select

EndSub

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!