Table Of Contents
Excel VBA Intersect
VBA Intersect is used to get a range object that is an intersection of two or more ranges. Therefore, one should supply a minimum of two ranges to find the intersecting range point. All the other arguments are optional based on the requirement.
Below is the syntax of the VBA INTERSECT formula.
- Arg1 as Range: First intersecting range.
- Arg2 as Range: Second intersecting range.
In the below examples, we will see some of the useful techniques.
Examples
Example #1
For example, use the below data.
Step 1: Declare the variable as a Variant.
Code:
Sub Intersect_Example() Dim MyValue As Variant End Sub
Step 2: Assign the value through the Intersect formula for this variable.
Code:
Sub Intersect_Example() Dim MyValue As Variant MyValue = Intersect( End Sub
Step 3: Select the first range as B2 to B9.
Code:
Sub Intersect_Example() Dim MyValue As Variant MyValue = Intersect(Range("B2:B9"), End Sub
Step 4: Select the second range from A5 to D5.
Code:
Sub Intersect_Example() Dim MyValue As Variant MyValue = Intersect(Range("B2:B9"),Range("A5:D5") End Sub
Step 5: We are testing with only two ranges here. Close the formula and select the method as a VBA Cell Address.
Code:
Sub Intersect_Example() Dim MyValue As Variant MyValue = Intersect(Range("B2:B9"), Range("A5:D5")).Address End Sub
Step 6: Show the value in the message box in VBA.
Code:
Sub Intersect_Example() Dim MyValue As Variant MyValue = Intersect(Range("B2:B9"), Range("A5:D5")).Address MsgBox MyValue End Sub
We are done and will see what we get in the message box.
We got the result as B5, i.e., the cell address of the intersection point of the supplied range.
Like this using the VBA INTERSECT method, we can do many more things.
Example #2
Select the Intersection Cell
To select the supplied range's intersection cell, use the code below.
Code:
Sub Intersect_Example2() Intersect(Range("B2:B9"), Range("A5:D5")).Select End Sub
It will select the intersection cell of the supplied range.
Example #3
Clear Content of the Intersection Cell: To clear the content of the intersection cell of the supplied range, use the code below.
Code:
Sub Intersect_Example2() Intersect(Range("B2:B9"), Range("A5:D5")).ClearContents End Sub
Example #4
Change the Cell Color Background and Font Color of Intersection Cell: Change the background color of the intersection cell and the font color of the intersection cell value using the code below.
Code:
Sub Intersect_Example2() Intersect(Range("B2:B9"), Range("A5:D5")).Cells.Interior.Color = rgbBlue Intersect(Range("B2:B9"), Range("A5:D5")).Cells.Font.Color = rgbAliceBlue End Sub
Change the Value of the Intersection Cell: Using the Intersect function, we can also change the value of that cell into something else.
In the above data, the intersection value of the range "B2:B9" and "A5:D5" is cell B5, marked with blue. Now by supplying this range to intersect function, we can change the value to something else.
The below code will change the value from 29398 to "New Value."
Code:
Sub Intersect_Example3() Intersect(Range("B2:B9"), Range("A5:D5")).Value = "New Value" End Sub
Run the code above. We will get the word "New Value" in place of 29398.
Like this, by using the Intersect function, we can play around with the middle position value of the supplied range.
Things to Remember
- In Excel, to get the intersect value of the range, we need to give space characters between two ranges.
- We can use VBA coding to highlight, format, delete or change, and do many other things to the intersection value.
- We will get the middle two values if the multiple rows and columns supply the Intersect function.