How to Use the Excel VBA Target Range – 4 Examples

What Is the Target Range in Excel VBA?

The Target in Excel VBA refers to the object that was last selected or clicked by the user.

How to Launch the VBA Editor in Excel

To enter a VBA code, go to the Developer tab and select Visual Basic or press Alt + F11.

Opening VBA window from the Developer tab

You can enable the Developer tab in Excel if it isn’t active.

After opening the VBA window, double-click the worksheet name.

Opening new code module in the VBA window

Select Worksheet from the upper left list and the event name from the upper right list in the code module.

Preparing code window for event


Example 1 – Showing Cell Address with a MsgBox to Change the Selection in the Target Range

  • Use this code in the module. Select any cell in the Target Range.

Excel VBA Code to show cell address when selection in target range

Code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Firstly, we are defining the target range
If Not Intersect(Target, Range("B5:D11")) Is Nothing Then
'Now let's do something for target range selection
MsgBox "The address of your selection is: " & Target.Address
End If
End Sub

Code Breakdown:

  • declares a private sub-procedure: Worksheet_SelectionChange. Target is a built-in variable of range type for this private sub.
  • checks if the Target (currently selected cell) is within B5:D11 using the Intersect function.
  • If the target is within our defined range, a MsgBox is displayed in the selected cell.

Read More: How to Use Target Address in Excel VBA


Example 2 – Highlight a Row for Any Change in Multiple Cells Within the Target Range

  • Enter the following code in the module. Change the cell property of cells in the Target Range.

Code to highlight row when changing cells in target range

Code:

Option Explicit
'Excel worksheet change event Range B5 to B11
Private Sub Worksheet_Change(ByVal Target As Range)
'Identifying if the change is in target range
If Not Intersect(Target, Range("B5:B11")) Is Nothing Then
'Changing Cell fill color for change in target range
Target.Cells(1, 1).Interior.ColorIndex = 15
Target.Cells(1, 2).Interior.ColorIndex = 15
Target.Cells(1, 3).Interior.ColorIndex = 15
End If
End Sub

Code Breakdown:

  • Option Explicit declares the variables.
  • checks  whether our Target (currently selected cell) is within B5:B11.
  • Cells(1,1) refers to the cells beside Target and fills them with color by setting the ColorIndex property to 15.

This is the output.

Highlighted row for change in target range

Read More: Excel VBA: How to Use Target Row


Example 3 – Insert Data from Another Range by Double Clicking Within the Target Range in Excel

  • Double-click cells in Target Range to fill them with data from another range in the worksheet.
  • Enter the code in the module. Double-click the cells within Target Range.

Code to insert data from another range for double-clicking on target range

Code:

'Excel worksheet double click change event Range B5 to D11
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Identifying if the double click is on target range
If Not Intersect(Target, Range("B5:D11")) Is Nothing Then
'taking data from another cell for double clicking
Target.Value = ActiveCell.Offset(10, 0).Value
End If
End Sub

Code Breakdown:

  • declares a private sub-procedure for double-clicking.
  • checks if the double-clicked Target is within B5:D11.
  • If it’s true, a value for the Target is set. The value is taken from a cell which is located using the Offset property.

This is the output.

Inserted data from another range for double-clicking on target range

Read More: How to Use Target Value in Excel VBA


Example 4 – Change the Font After Selecting Any Cell in the Target Range

  • Enter the following code in the module. Select cells in the Target Range.

Code to change the font for selecting cell

Code:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Identifying if the change in selection is occurring inside target range
If Not Intersect(Target, Range("B5:B11")) Is Nothing Then
'Changing font for selection change inside target range
Range(Target.Address).Font.Bold = True
End If
End Sub

The code checks if the Target (selected cell) is within B5:B11. If that’s true, the Font.Bold property is applied to the selected cell.

Changed font for selection within target range

Read More: Excel VBA Target Cell


How to Use a Single Cell as a Target Address in Excel VBA

Here, B8 is set as Target.

  • Enter the code in the module. Select B8.

Code to use single cell as target range

Code:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'Checking if the change in target cell
If Not Intersect(Target, Range("B8")) Is Nothing Then
'Performing color fill for cells for change in target cell
Target.Cells(1, 1).Interior.ColorIndex = 20
Target.Cells(1, 2).Interior.ColorIndex = 20
Target.Cells(1, 3).Interior.ColorIndex = 20
End If
End Sub

Code Breakdown:

  • checks if the Target (Selected cell) is B8 using the Intersect property.(You can also use the  “Target.Address = B8” condition with an If statement).
  • If that is true, the Target cell and the cells beside it are selected with the Cells property, and the ColorIndex property set to 20, highlights those cells.
Chanigng row fill color for change in single cell as target range

The value of B8 changed.


Things to Remember

  • Don’t forget to save the workbook as xlsm after entering the VBA code.

Download Practice Workbook

Download the practice workbook here.

 

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Mehedi Hasan Shimul
Mehedi Hasan Shimul

Md. Mehedi Hasan, with a BSc in Electrical & Electronic Engineering from Bangladesh University of Engineering and Technology, holds a crucial position as an Excel & VBA Content Developer at ExcelDemy. Driven by a deep passion for research and innovation, he actively immerses himself in Excel. In his role, Mehedi not only skillfully addresses complex challenges but also exhibits enthusiasm and expertise in gracefully navigating tough situations, emphasizing his steadfast commitment to consistently deliver exceptional and quality content.... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo