VBA won't work when password protected

I'm having a hard time trying to figure out how to make my vba work when the worksheet protection is on. It highlights the entire row of the table when the clicks on that particular row. I use a Worksheet_SelectionChange but it doesn't seem to work when the password protection is on. Any Suggestions?

Public Sub HighlightTableRow(Target As Excel.Range)

    Dim t As ListObject
    Dim lngInTable As Long
    Dim c As Long
    Const COLOR_SELECT = xlThemeColorAccent1
    Const COLOR_LIGHTER = 0.4
    On Error Resume Next
    If Target.Interior.Pattern = xlPatternSolid Then Exit Sub
    For Each t In Target.Parent.ListObjects
        c = c + 1
        If Not Intersect(Target, t.DataBodyRange) Is Nothing Then
            lngInTable = c
        End If
        t.Range.Interior.Pattern = xlNone
    If lngInTable = 0 Then Exit Sub
    With Target.Parent.ListObjects(lngInTable)
        With .Range.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        With .DataBodyRange
            With .Resize(Target.Rows.Count).Offset(Target.Row - .Row).Interior
                .ThemeColor = COLOR_SELECT
                .TintAndShade = 1 - COLOR_LIGHTER
            End With
        End With
    End With

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    On Error Resume Next
    HighlightTableRow Target

End Sub

Open in new window

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.

You will have to unprotect the sheet before changes are made then protect it again at the end of the procedure.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Target.Parent.Unprotect ("YourPassword")
    On Error Resume Next
    HighlightTableRow Target
    Target.Parent.Protect ("YourPassword")
End Sub

Open in new window


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
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.