As the word itself suggests union means joining one or more things. In VBA Union means joining two or more ranges together. This function is similar to the range function in excel. This is the most common situation in our work when we need to combine one or more ranges with each other. Union function comes very handily in those situations.
VBA Union functions are used to combine one or more ranges as explained above. We can use this function to combine ranges that have some kind of common criteria. For example, if our data has value less than a specific value we can use this function to combine those ranges and highlight them.
Syntax of VBA Union in Excel
The syntax for Union function is as follows:
So for example, if we want to combine a range A1: A5 and B1: B5 we will use the following formula,
Union (Range (“A1:A5”), Range (“B1:B5”)
We can do much more with this function and we will see through various examples on how to use this function in VBA.
First, let us make sure that we have a developer’s tab enabled from the files tab in the options section so that we can start using VBA in excel.
How to Use VBA Union Function in Excel?
We will learn how to use a VBA Union function with few examples in excel.
Example #1 – VBA Union
In the first example let us try to select two ranges together. Let us select A1:A5 and B1:B5 range together in this example.
Follow the below steps to use VBA Union function in Excel:
Step 1: Of course we need to open VB editor from visual basic which is in the developer’s tab.
Step 2: Now once we are in VB Editor go ahead and insert a new module from the insert section. The module we have inserted double click on it so that we can start writing code.
Step 3: Once we are in the code window, name the macro as follows,
Sub sample() End Sub
Step 4: Since we will be working with sheet 1 we need to activate it first in order to use its properties.
Sub sample() Worksheets("Sheet1").Activate End Sub
Step 5: Now we will use union function to combine the two ranges we have discussed above with the following code.
Sub sample() Worksheets("Sheet1").Activate Application.Union(Range("A1:A5"), Range("B1:B5")).Select End Sub
Step 6: Once we execute the code above we can see in sheet 1 that those two ranges are in our selection. Press F5 or do it manually from run button to see the following result.
In the above example, we have only selected the two ranges but we can do much more which we will learn in the next examples.
Example #2 – VBA Union
Now in this example let us select two ranges as above together and change their interior color. We can change format or change values once we combine and select the ranges together.
Step 1: Go to Insert Menu and click on the module
Step 2: Declare a name for the subfunction for the second example,
Sub Sample1() End Sub
Step 3: Now let us activate sheet 2 first since we are going to use the properties of sheet 2 in this example.
Sub Sample1() Worksheets("Sheet2").Activate End Sub
Step 4: Combine two ranges A1:B5 and C1:D5 with range function and change the interior color to dark red by the following code.
Sub Sample1() Worksheets("Sheet2").Activate Application.Union(Range("A1:B5"), Range("C1:D5")).Interior.Color = 255 End Sub
Step 5: Execute the above and see the result in sheet 2 as follows,
We have changed the color of the ranges after combining them as we can see that they are still in selection.
Example #3 – VBA Union
Now let use union function to display the address after combining ranges. We will combine range A1:C4 and E1:F4 and display the address in the Immediate window. An immediate window is just below our code window or we can press CTRL + G to bring it up.
Step 1: Go to Insert Menu and click on the module,
Step 2: Name the macro name for this third example.
Sub Sample2() End Sub
Step 3: Declare two variables as a range in the next step as follows.
Sub Sample2() Dim rng1 As Range Dim item As Range End Sub
Step 4: Now set an rng1 variable as the union of the range A1: C4 and E1: F4 as follows,
Sub Sample2() Dim rng1 As Range Dim item As Range Set rng1 = Union(Range("A1:C4"), Range("E1:F4")) End Sub
Step 5: Now use for loop to bring the address of these cells from the combined ranges by the following code,
Sub Sample2() Dim rng1 As Range Dim item As Range Set rng1 = Union(Range("A1:C4"), Range("E1:F4")) For Each item In rng1 Debug.Print item.Address Next item End Sub
Step 6: Once we run the above code we can see the result in the immediate window as follows,
Application of VBA Union
VBA union is used by the following syntax:
Here we can use as many ranges as we require.
Things to Remember
There are few things which we need to remember about the union in VBA:
- The union is used to combine two or more ranges together.
- The ranges we give to the function must exist to avoid an error.
- Instead of Application. Union we can simply use the union as we are working in excel itself.
This is a guide to VBA Union. Here we discuss how to use Excel VBA Union Function along with practical examples and downloadable excel template. You can also go through our other suggested articles –