How To Change Color Of The Row With A Click Using VBA In Microsoft Excel 2010

In this article, you will learn how to change color of the row with a click.

 

Click on Developer tab

From Code group select Visual Basic
 
img1
 
Enter the following code in the current worksheet (sheet1 in our example)

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim rownumber As Integer

rownumber = ActiveCell.Row

    If ActiveCell.Value <> “” Then

        Range(“A” & rownumber & “:D” & rownumber).Interior.ColorIndex = 6

    End If

End Sub
 
img2
 
The SelectionChange event will get activated every time the user selects any cell & it will change the color from white to yellow color. If active cell is empty then, the code will not run.

If we click on cell B3 then, the row will be highlighted in yellow color. Refer below shown snapshot
 
img3
 
Here, you can see that the formula bar shows selected cell i.e. cell B3

If we select cell A8 then, row 8 will get highlighted. Refer below shown snapshot.
 
img4
 
In case you want to highlight only single row at a time then you need to add one line to the previous code.

Added code:

Range(“A1:D13″).Interior.ColorIndex = xlNone

The code becomes:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim rownumber As Integer

rownumber = ActiveCell.Row

    If ActiveCell.Value <> “” Then

    Range(“A1:D13″).Interior.ColorIndex = xlNone

    Range(“A” & rownumber & “:D” & rownumber).Interior.ColorIndex = 6

    End If

End Sub

 

Now, only one row will get highlighted at one point of time.

 

In this way, you can highlight the rows with a single click, using VBA code.
 
 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.

Visit Us On TwitterVisit Us On FacebookVisit Us On Google PlusVisit Us On Youtube