Forum Discussion

packie's avatar
packie
Brass Contributor
May 20, 2023
Solved

Highlight row and column when cell is selected

Hi,

 

Just need a little help to try out coding a sheet where the selected row and column automatically highlights.

 

I could perhaps do it using conditional formatting but I would like to try it out using VBA 

 

Thank you in advance for any help you can offer.

 

 

  • packie 

    Right-click the sheet tab.

    Select View Code from the context menu.

    Copy the following code into the worksheet module:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Dim MyColor As Long
        MyColor = RGB(224, 255, 224) ' a light green - change as desired
        Application.ScreenUpdating = False
        ' Clear the color of all the cells
        Cells.Interior.ColorIndex = xlColorIndexNone
        With ActiveCell
            ' Highlight the entire row and column that contain the active cell
            .EntireRow.Interior.Color = MyColor
            .EntireColumn.Interior.Color = MyColor
        End With
        Application.ScreenUpdating = True
    End Sub

    Warning: This will remove all existing fill color from the entire sheet before highlighting the row and column of the active cell!

    This will apply to that specific sheet only.

    It's possible to create a variant that will apply to all sheets in the workbook.

    • packie's avatar
      packie
      Brass Contributor
      "Warning: This will remove all existing fill color from the entire sheet before highlighting the row and column of the active cell!"

      I have fill color on the sheet which I do not want to loose using conditional formatting.

Resources