Vba code with unprotect that worke on Excel 2010 doesn't work on Excel 2016

Copper Contributor

Hi,

 

I have a macro that worked on Excel 2010 and doesn't work on Excel 2016. In this macro I protect the workbook and all the sheets "Before close" and unprotect depending on the user that opens the file on "Open". The macro uses the same variable to protect and unprotect but when I ran it on Excel 2016 it protected the workbook and the sheets, and unprotected the sheets ok but didn't unprotect the workbook, it said that the password was wrong. I've tried also from the menu and the structure of the workbook cannot be unprotected.

 

The lines of code for protecting are:

 

For Each ws In ActiveWorkbook.Worksheets
ws.Protect Password:=clau
Next ws
ActiveWorkbook.Protect Password:=clau, structure:=True, Windows:=False

 

The lines for unprotecting are:

 

If (Application.UserName = "XXX" Or Application.UserName = "YYY") Then
ActiveWorkbook.Unprotect Password:=clau
For Each ws In ActiveWorkbook.Worksheets
ws.Unprotect Password:=clau
Next ws
End If

 

The error is the 1004 and it says that is caused by worng password but "clau" is the same, I don't understand what's happening... What can be wrong? How can I unprotect the workbook now?

 

Thanks.

0 Replies