How to highlight row of the selected cell programmatically
Today I would like to share with you these small event handler procedures that make it easier for you to read data on the same row or column. These procedures highlight a row and column of the selected cell. Make sure you don't have colored cells in your worksheet as the first four procedures remove colors from cells.
The last procedure is different, it creates borders on the same row as the selected cell. You can use this procedure if you have colored cells on your sheet.
What's on this page
1. How to highlight a row automatically using event macro?
This event code highlights the entire row of the selected cell. I have chosen the color green to highlight the entire row.
How to add code to your workbook
- Press with right mouse button on on the worksheet name and select "View Code". This opens the Visual Basic Editor and the corresponding module.
- Copy the VBA code below and paste it to the module.
- Exit the VB Editor and go back to Excel.
VBA Code
'Eventcode procedure name, you can't change this line Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. 'These lines removes cell colors With Cells.Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With 'These lines applies cell colors to all cells in row of selected cell With Rows(Selection.Row).Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 5296274 .TintAndShade = 0 .PatternTintAndShade = 0 End With End Sub
Microsoft Docs: Cells | Interior | Pattern | TintAndShade | PatternTintAndShade | PatternColorIndex | Color | Rows
2. Highlight column of the selected cell
This Event code highlights the entire column of the selected cell.
How to add code to your workbook
VBA Code
'Event code Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. 'These lines removes cell colors With Cells.Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With 'These lines applies cell colors to all cells in column of selected cell With Columns(Selection.Column).Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 5296274 .TintAndShade = 0 .PatternTintAndShade = 0 End With End Sub
Microsoft Docs: Cells | Interior | Pattern | TintAndShade | PatternTintAndShade | PatternColorIndex | Color | Columns | Selection | Selection.Column
3. Highlight row and column of the selected cell
This event code highlights the entire column and row of the selected cell.
How to add code to your workbook
VBA code
'Event code Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. 'These lines removes cell colors With Cells.Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With 'These lines applies cell colors to all cells in row of selected cell With Rows(Selection.Row).Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 5296274 .TintAndShade = 0 .PatternTintAndShade = 0 End With 'These lines applies cell colors to all cells in column of selected cell With Columns(Selection.Column).Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 5296274 .TintAndShade = 0 .PatternTintAndShade = 0 End With End Sub
Microsoft Docs: Cells | Interior | Pattern | TintAndShade | PatternTintAndShade | PatternColorIndex | Color | Columns | Selection | Selection.Column |Â Rows | Selection.Row
4. Highlight rows and columns of multiple selected cells
This event code allows you to highlight entire columns and rows of multiple selected cells. Press and hold the SHIFT key to select multiple cells with your mouse.
How to add code to your workbook
'Event code Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. 'These lines removes cell colors With Cells.Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With 'Iterate through all cells in selection For Each cell In Selection 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. 'These lines removes cell colors With Rows(cell.Row).Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 5296274 .TintAndShade = 0 .PatternTintAndShade = 0 End With 'These lines applies cell colors to all cells in column of selected cell With Columns(cell.Column).Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 5296274 .TintAndShade = 0 .PatternTintAndShade = 0 End With 'These lines applies cell colors to all cells in row of selected cell With cell.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 5287936 .TintAndShade = 0 .PatternTintAndShade = 0 End With Next cell End Sub
Microsoft Docs: Cells | Interior | Pattern | TintAndShade | PatternTintAndShade | PatternColorIndex | Color | Columns | Selection | Rows
5. Apply borders to the row of the selected cell
This event procedure applies borders to the row. It does not remove colors from cells instead it removes all borders every time you select a new cell.
How to add code to your workbook
'Event code Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. 'These lines removes cell colors With Cells .Borders(xlDiagonalDown).LineStyle = xlNone .Borders(xlDiagonalUp).LineStyle = xlNone .Borders(xlEdgeLeft).LineStyle = xlNone .Borders(xlEdgeTop).LineStyle = xlNone .Borders(xlEdgeBottom).LineStyle = xlNone .Borders(xlEdgeRight).LineStyle = xlNone .Borders(xlInsideVertical).LineStyle = xlNone .Borders(xlInsideHorizontal).LineStyle = xlNone End With 'These lines enables a top cell border to all cells in row of selected cell With Rows(Selection.Row).Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With 'These lines enables a bottom cell border to all cells in row of selected cell With Rows(Selection.Row).Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With End Sub
Microsoft Docs: Cells | Interior | Pattern | TintAndShade | PatternTintAndShade | PatternColorIndex | Color | Columns | Selection | Rows | Cells.Borders | LineStyle
Macro category
Table of Contents Excel monthly calendar - VBA Â Calendar Drop down lists Headers Calculating dates (formula) Conditional formatting Today Dates […]
Table of contents Save invoice data - VBA Invoice template with dependent drop down lists Select and view invoice - […]
This blog post demonstrates how to create, populate and change comboboxes (form control) programmatically. Form controls are not as flexible […]
With end with statement category
This article demonstrates a User Defined Function that allows you to extract cell references from a formula in a given […]
Terry wants to make a different sized maze and I think that is a great idea. Perhaps you remember that I […]
Excel categories
13 Responses to “How to highlight row of the selected cell programmatically”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
Great job and is very useful
Thank you very much Mr. (Oscar)
Thank you!
Work more than wonderful, because you are a wonderful person
Dear Oscar
I want to highlight the row if the checkbox in my customized menu is pressed on.
please help & provide code.
Dear Oscar
In your first example Excel highlights all rows until last column (XFD). I need highlight from column A to column R. Is it possible?
Thanks!
Hi Oscar,
Thanks a lot for all these wonderful macros.
I wanna know how the codes will be if I want to maintain the headers with blue color and the color changes happens only to the cells below the header.
hello
I want program with VBA
with press with left mouse button on a picture count into cell
thanks
Hi Oscar,
Is it possible to highlight a row but only within a table?
Thanks
Nilhan
Hi Oscar,
First i want to tell you Big Thank You for these examples and working codes.
I would like to as, is there a way to maintain all other colors on my sheet and still get benefit from "Highlight row and column"/code/?
If you can just send me this tipe of code on my email i will be very thankful.
BR,
Martin
Dear Sir.
Big thanks for examples and working codes is very useful
please explan how to change colour
Sanjaya
Sanjaya,
I believe you change this:
Interior.Color property (Excel)
Thanks a lot for your sharing.
How can we use this as default in excel?
Zin Thida Win
You can save it to your personal workbook and always have access to it.
Copy your macros to a Personal Macro Workbook
Add your personal Excel Macros to the ribbon