Unprotecting not working - 1004 error

plsm5882

New Member
Joined
Apr 22, 2016
Messages
20
Hi.

Unprotecting is failing for some reason. I am using UserInterfaceOnly:=True but it still complains.

Public Sub SaveWB()
Dim wSh As Worksheet
Set wSh = ThisWorkbook.Worksheets("Summary")
wSh.Unprotect

With Application
.DisplayAlerts = False
.ScreenUpdating = False
On Error Resume Next
wSh.Range("C5").Value = Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) ' Fails here with 1004 error stating the sheet is still protected
If err.Number = 0 Then
ThisWorkbook.Save
If err.Number <> 0 Then
Call ErrMsg(err.Number, err.Description, "Error Saving", "The spreadsheet could not be saved. Please make sure the location where you open it from is still available.")
On Error GoTo 0
End If
End If
.ScreenUpdating = True
.DisplayAlerts = True
End With

wSh.Protect UserInterfaceOnly:=True

Set wSh = Nothing
End Sub

Hope someone has some insights.

Thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Did you remove the password from your code before posting, or did you forget to put it in?

It should be
VBA Code:
wSh.Unprotect Password:= "your password goes here"
Although I would have expected a prompt for the password before it got to the problem line if that was the case.
 
Upvote 0
No, I dont use a password. I simply use the protection to stop accidental deletion of data. Unprotect works everywhere else. Is it not optional then?

Anyway, I've tested if it makes a difference to the error and unfortunately it doesn't.
 
Last edited:
Upvote 0
If there is no password then as far as I can see, your code should have worked, I've tried to make it fail without success.

If you run
VBA Code:
Sub Test()
Dim wSh As Worksheet
Set wSh = ThisWorkbook.Worksheets("Summary")
wSh.Unprotect
End Sub
does it remove the protection and allow you to edit the sheet manually?
 
Upvote 0
It does. I protect and unprotect 7 or 8 times in total, this sheet several other times and other sheets too. They all work except for this one. It's odd and very frustrating...

I'll try some error handling...

Also, unly using interface only protection, instead of unprotecting and protecting, could I unlock and lock the cell?

As far as I know I shouldn't have to do either when using interface only...
 
Last edited:
Upvote 0
Interface only protection will only work after it has been unlocked once.

Common practice would be to use the Workbook_Open code event to sort the protection out so that it is ready for any other code.
VBA Code:
Private Sub Workbook_Open()
Dim ws As Workbook
For Each ws In ThisWorkbook.Worksheets
    ws.Unprotect
    ws.Protect UserInterfaceOnly:=True
Next
End Sub
 
Upvote 0
Yep, already doing that. I assume you meant "Dim ws As Worksheet"?

Could it be because I call it from:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call SaveWB
End Sub
 
Last edited:
Upvote 0
In that case, I see no reason for the problem that you're having, I have tested your code and it works fine.
As you say that the problem is only on one sheet, it could be a problem with that specific sheet, I would suggest creating a new sheet and see if it works on that.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top