Highlight the Active Row and Column Use Excel VBA

When you looking at a large worksheet with numerous data, it would be nice if Excel automatically highlighted the active cell row and column so that you can easily read the data to avoid misreading them. See following screenshots:

Highlight the Active Row and Column

The following VBA code examples show ways to highlight the active cell or the rows and columns that contain the active cell.

Highlight the Active Cell VBA Code

The following VBA code example clears the color in all the cells on the worksheet by setting the ColorIndex property equal to 0, and then highlights the active cell by setting the ColorIndex property equal to 6 (Yellow).

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Application.ScreenUpdating = False
    ' Clear the color of all the cells
    Cells.Interior.ColorIndex = 0
    ' Highlight the active cell
    Target.Interior.ColorIndex = 6
    Application.ScreenUpdating = True
End Sub

Highlight the Entire Row and Column VBA Code

The following VBA code example clears the color in all the cells on the worksheet by setting the ColorIndex property equal to 0, and then highlights the entire row and column that contain the active cell by using the EntireRow and EntireColumn properties.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Resume Next
    Application.ScreenUpdating = False
    ' Clear the color of all the cells
    Cells.Interior.ColorIndex = 0
    If Target.Cells.Count > 1 Then Exit Sub
    With Target
        ' Highlight the entire row and column that contain the active cell
        .EntireRow.Interior.ColorIndex = 6
        .EntireColumn.Interior.ColorIndex = 6
    End With
    Application.ScreenUpdating = True
End Sub

Highlight the Row and Column Within the Current Region VBA Code

The following VBA code example clears the color in all the cells on the worksheet by setting the ColorIndex property equal to 0, and then highlights the row and column that contain the active cell, within the current region by using the CurrentRegion property of the Range object.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Resume Next
    ' Clear the color of all the cells
    Cells.Interior.ColorIndex = 0
    If IsEmpty(Target) Or Target.Cells.Count > 1 Then Exit Sub
    Application.ScreenUpdating = False
    With ActiveCell
        ' Highlight the row and column within the current region
        Range(Cells(.Row, .CurrentRegion.Column), Cells(.Row, .CurrentRegion.Columns.Count + .CurrentRegion.Column - 1)).Interior.ColorIndex = 6
        Range(Cells(.CurrentRegion.Row, .Column), Cells(.CurrentRegion.Rows.Count + .CurrentRegion.Row - 1, .Column)).Interior.ColorIndex = 6
    End With
    Application.ScreenUpdating = True
End Sub

Highlight the Entire Row and Column with double-click VBA Code

The following VBA code highlights the entire row and column with a simple double-click.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Step 1:  Declare Variables
    Dim strRange As String
'Step2:  Build the range string
    strRange = Target.Cells.Address & "," & _
               Target.Cells.EntireColumn.Address & "," & _
               Target.Cells.EntireRow.Address
'Step 3: Pass the range string to a Range
    Range(strRange).Select
End Sub

How to Use This Macro

To use this macro, you can copy and paste it into the code window:

  1. Activate the Visual Basic Editor by pressing ALT+F11.
  2. In the Project window, find your project/workbook name and click the plus sign next to it in order to see all the sheets.
  3. Click on the sheet from which you want to trigger the code.
  4. Copy and paste the VBA code into the code window.
    Insert Macro Code

Comments

  1. says

    I like option #2 “Highlight the Entire Row and Column VBA Code”

    I don’t want to destroy exisitng formating, do you have a code for that, that you would like to share?

    Thank you.
    Vonnie

Leave a Reply

Your email address will not be published. Required fields are marked *