Forum Discussion
packie
May 20, 2023Brass Contributor
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.
There is no code whatsoever in that workbook. Perhaps you copied the code to a module in another workbook?
Here is the workbook with the code. You may have to unblock the file after downloading it.
- peiyezhuBronze Contributor
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.
- packieBrass 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.