Highlight Current Row in Excel (VBA)
Hi All,
Here is a way to highlight the current row.
This code goes in worksheet module. Right click Tab Name > View code and paste the code there on the VBE window.
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'// Developed by Kris @ ExcelFox.com
Dim x, nmRow As Name
'if A1 holds '0', the macro won't fire
If Me.Range("A1") = 0 Then Exit Sub 'adjust the flag cell
On Error Resume Next
Set nmRow = ThisWorkbook.Names("tRow")
On Error GoTo 0
Const HighlightColor As Long = 6750207 'Adjust the highlight color
If nmRow Is Nothing Then
Set nmRow = ThisWorkbook.Names.Add("tRow", Target.Row & "|" & Target.EntireRow.Interior.Color, 0)
Target.EntireRow.Interior.Color = HighlightColor
Else
x = Split(Evaluate("tRow"), "|")
Me.Rows(CLng(x(0))).Interior.Color = IIf(CLng(x(1)) = 16777215, -4142, CLng(x(1)))
nmRow.RefersTo = Target.Row & "|" & Target.EntireRow.Interior.Color
Target.EntireRow.Interior.Color = HighlightColor
End If
End Sub
Note: This code ensures that you won't lose your row color.
Enjoy !
code w/o erasing original highlighted color
is there a code that does the same thing (highlight active row) with out removing original highlighted color after cell is no longer active?
Quote:
Originally Posted by
Rick Rothstein
Put this code in the workbook's code module (do not put it in any of the worksheet modules)...
Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Cells.Interior.ColorIndex = xlColorIndexNone
Target.EntireRow.Interior.ColorIndex = 6
End Sub
If you are not sure where the workbook module is, look over at the Project Explorer window within the VBA editor (CTRL+R if you do not see it) and then double-click the entry labeled ThisWorkbook... that will open the workbook code module... copy paste the code in it. Remember though, with this code you cannot have any other manually colored cells as the code will remove their colors when executing.