how to color active row automatically

What is the easiest way to be able to color the active row automatically based on active cell.

so wherever i select, it should color it and when i click in another cell it should color that and remove the color from previously selected row.

I have excel 2016,  i searched in internet but the results were either not working or too slow.
LVL 6
FloraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Put this code in the worksheet's SelectionCHange event.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static lngLastRow As Long
If lngLastRow > 0 Then
    Cells(lngLastRow, "A").EntireRow.Style = "Normal"
End If
ActiveCell.EntireRow.Style = "Note"
lngLastRow = Target.Row
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FloraAuthor Commented:
Thanks Martin.

it has one issue, i have a query from Analysis CUBE of SQL.  the vba triggers the update of query on each change.  how can i disable the refresh of the query ?
0
Martin LissOlder than dirtCommented:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Static lngLastRow As Long

Application.EnableEvents = False

If lngLastRow > 0 Then
    Cells(lngLastRow, "A").EntireRow.Style = "Normal"
End If
ActiveCell.EntireRow.Style = "Note"
lngLastRow = Target.Row

Application.EnableEvents = True
End Sub

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

FloraAuthor Commented:
it is ok now.  thanks Martin.  your solution worked perfectly.
0
Martin LissOlder than dirtCommented:
It wouldn't hurt to use my updated code but in any case you’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
              Experts Exchange Top Expert VBA (current)
0
Martin LissOlder than dirtCommented:
If you want to start with a "clean slate" when you open the workbook then add this in the code for ThisWorkbook. Change line 6 if necessary to match your sheet name.

Private Sub Workbook_Open()
Dim lngRow As Long

Application.EnableEvents = False

With ThisWorkbook.Sheets("Sheet1")
    For lngRow = 1 To 99999
        If .Cells(lngRow, "A").Style = "Note" Then
            .Cells(lngRow, "A").EntireRow.Style = "Normal"
            Exit For
        End If
    Next
End With

Application.EnableEvents = True
End Sub

Open in new window

0
Rob HensonFinance AnalystCommented:
You can also achieve this with Conditional Formatting which has the advantage that it doesn't overwrite any existing manual formatting.

However, CF only changes on a calculation event which changing selection doesn't cause so would still need a VBA script like Martin has provided but it would be a simple one liner forcing Calculation.

Select the area where you want the row highlight to occur and use this formula in Conditional Formatting:

=CELL("row")=CELL("row",A1)

Formula assumes selected area starts at A1; change the A1 reference to the top left cell of the selected area.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.