excelvbapassword-protection

I am having trouble running a macro on a protected sheet


I am trying to run a simple macro by button click, but when I click the button while the sheet is protected, nothing happens. When I unprotect the sheet and click the button, the macro runs without issue.

Here is the macro is question:

Sub ApproveAllPending()
    Columns("B").Replace What:="Pending", _
                            Replacement:="Approved", _
                            LookAt:=xlPart, _
                            SearchOrder:=xlByRows, _
                            MatchCase:=False, _
                            SearchFormat:=False, _
                            ReplaceFormat:=False
End Sub




I saw a few posts with me.protect UserInterfaceOnly:=True, but I wasn't able to get that working.

Edit:

I tried adding this as well, and it's not working either.

Sheet1.Protect Password:="password", UserInterFaceOnly:=True


Solution

  • Option Explicit
    Sub ApproveAllPending()
        Sheet1.Unprotect Password:="mypw"
        Sheet1.Columns("B").Replace What:="Pending", _
                                Replacement:="Approved", _
                                LookAt:=xlPart, _
                                SearchOrder:=xlByRows, _
                                MatchCase:=False, _
                                SearchFormat:=False, _
                                ReplaceFormat:=False
    
        Sheet1.Protect Password:="mypw"
    End Sub
    
    Option Explicit
    
    Private Sub Workbook_Open()
        Sheet1.Protect Password:="mypw", UserInterFaceOnly:=True
    End Sub