Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Excel-VBA-how to highlight target row in table?

The following code works to highlight row 1 in ListObject(2)

Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Cells.Count < 1 Then Exit Sub 
    
    Application.ScreenUpdating = False
    
    'Clear the color of all cells
    Cells.Interior.ColorIndex = 0
    
    Set oList = ActiveSheet.ListObjects(2)
    With Target
        'Highlight row and column of the selected cell
        .EntireColumn.Interior.ColorIndex = 24
        oList.ListRows(1).Range.Interior.ColorIndex = 38
    End With

End Sub

Question: How can I highlight the target row instead?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use Intersect:

Dim rng As Range
Set rng = Intersect(Target.EntireRow, oList.DataBodyRange)

If Not rng Is Nothing Then
    rng.Interior.ColorIndex = 38
End If
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading