VBA error 1004-password incorrect caps lock on

soleturnes

New Member
Joined
Sep 26, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi! I have an excel file with some macros, which i've been using for a while, that protect and unprotect the workbook, with the password written in the code, but for some reason it isn´t recognizing now the password.
The password hasn´t been changed.
Could someone please help me?

I'll post my code below, but it works fine with other files.
1664214450558.png


Code to unprotect:
ActiveWorkbook.Protect Password:="xx", Structure:=False

I've already tried without success:
ThisWorkbook.Protect Password:="xx", Structure:=False
Workbooks("NAME").Protect Password:="xx", Structure:=False
ActiveWorkbook.UnProtect Password:="xx"
ActiveWorkbook.UnProtect (["xx"])
Opening it in Safe Mode

When trying to unlock the workbook in the review tab I get the same error

Thanks!
 
I don't think someone could change the password because the VBA is locked with another password that only I know. Could someone changed it in the review tab?
How could it became corrupt? :( is there a way to fix it?

Thanks!!

Unfortunately corruption still happens once in a while. It's why I put backup copy code in anything I regard as mission critical. What you should do is immediately attempt to locate your 'last known good' copy to salvage what you can. This many involve getting anyone you sent a copy to, to send you a copy back. Forum rule #6 prohibits any discussion about how to defeat passwords so exploring any recovery strategies here is out of bounds.

Lastly, and this will sound stupid, but if you have not rebooted your PC since this problem occurred, do so now, then try one more time to access the file.
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Unfortunately corruption still happens once in a while. It's why I put backup copy code in anything I regard as mission critical. What you should do is immediately attempt to locate your 'last known good' copy to salvage what you can. This many involve getting anyone you sent a copy to, to send you a copy back. Forum rule #6 prohibits any discussion about how to defeat passwords so exploring any recovery strategies here is out of bounds.

Lastly, and this will sound stupid, but if you have not rebooted your PC since this problem occurred, do so now, then try one more time to access the file.
Yup I did.. thanks for the help anyway"
 
Upvote 0
Another thing to consider is a log sheet of some description. During a before close event you can test if the password in use matches what it should be, if not log the error with user details, better still have it send you a message via CDO mail.
Thanks for the tip! I'm not sure if I understand you, the password isn´t stored in a variable it's written in the code, how should I compare it with what it should be?
 
Upvote 0
Thanks for the tip! I'm not sure if I understand you, the password isn´t stored in a variable it's written in the code, how should I compare it with what it should be?

You can test if the sheet or workbook are protected using the relevant method

VBA Code:
Function IsSheetProtected(TargetSheet As String) As Boolean
IsSheetProtected = Sheets(TargetSheet).ProtectContents
End Function

Function IsWorkbookProtected() As Boolean
With ThisWorkbook
    IsWorkbookProtected = (.ProtectStructure Or .ProtectWindows)
End With
End Function

In this instance I've tested a single sheet, but this could be applied to all or an array of sheets.

VBA Code:
Sub Test()
Const pass As String = "YourPassword"                       'You can store your password as a constant
Dim sh As String

sh = "TheNameOfYourSheet"                                   'set sh for testing, but this could be in a loop

On Error Resume Next                                        'an error will occur if the password is incorrect, avoid that

Sheets(sh).Unprotect pass                                   'attempt to unprotect the sheet with your designated password
If IsSheetProtected(sh) Then                                'test if the sheet is still protected
    'do something if the sheet is still protected after
    'unprotecting it with your password
End If
Sheets(sh).Protect pass                                     'in the event the sheet was fine the reapply the protection
End Sub

Details on using CDO mail can be found at Ron De Bruin's resource website.
 
Upvote 0
Solution
You can test if the sheet or workbook are protected using the relevant method

VBA Code:
Function IsSheetProtected(TargetSheet As String) As Boolean
IsSheetProtected = Sheets(TargetSheet).ProtectContents
End Function

Function IsWorkbookProtected() As Boolean
With ThisWorkbook
    IsWorkbookProtected = (.ProtectStructure Or .ProtectWindows)
End With
End Function

In this instance I've tested a single sheet, but this could be applied to all or an array of sheets.

VBA Code:
Sub Test()
Const pass As String = "YourPassword"                       'You can store your password as a constant
Dim sh As String

sh = "TheNameOfYourSheet"                                   'set sh for testing, but this could be in a loop

On Error Resume Next                                        'an error will occur if the password is incorrect, avoid that

Sheets(sh).Unprotect pass                                   'attempt to unprotect the sheet with your designated password
If IsSheetProtected(sh) Then                                'test if the sheet is still protected
    'do something if the sheet is still protected after
    'unprotecting it with your password
End If
Sheets(sh).Protect pass                                     'in the event the sheet was fine the reapply the protection
End Sub

Details on using CDO mail can be found at Ron De Bruin's resource website.
Hello! So I tested this for the workbook as I could still unprotect the sheets (which have the same password as the workbook). The function (Isworkbookprotected) was true, but then I added the sub test for the workbook and it worked, the workbook was unprotected finally! The only thing that I can think of being different is the storing the password as a constant....
Thank you Dave!! :)
 
Upvote 0

Forum statistics

Threads
1,214,430
Messages
6,119,438
Members
448,897
Latest member
dukenia71

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