Link to home
Start Free TrialLog in
Avatar of Southern_Gentleman
Southern_GentlemanFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of FamousMortimer
FamousMortimer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial