I have this code:
Private Sub Workbook_Open()
Dim wSheet As Worksheet
Dim my_code As String
Dim x As Long, y As Integer
Dim Pass_word, Prev_pass As String
my_code = Val(Format(Date, "#")) * 397
Pass_word = create_pass(my_code)
For Each wSheet In Worksheets
On Error GoTo errhandler
wSheet.protect Password:=Pass_word, _
UserInterFaceOnly:=True
Next wSheet
Exit Sub
errhandler:
x = 1
Do
my_code = Val(Format(Date - x, "#")) * 397
Prev_pass = create_pass(my_code)
For Each wSheet In Worksheets
On Error GoTo move
wSheet.Unprotect (Prev_pass)
Next wSheet
move:
If Err.Number <> 0 Then
x = x + 1
y = 0
Err.Clear
Else
For Each wSheet In Worksheets
wSheet.protect Password:=Pass_word, _
UserInterFaceOnly:=True
Next wSheet
y = 1
End If
Loop Until y = 1
Resume Next
End Sub
What I want to do is to protect sheets inside a workbook with a dynamic password that changes everyday based on the date.
What i'm having trouble with is how to change the set password every day.
I added the errhandler routine but it doesn't work.
create_pass is a function i created to generate the coded pass and i already tested it and it returns the right value.
Error occurs in this somewhere here:
For Each wSheet In Worksheets
On Error GoTo move
wSheet.Unprotect (Prev_pass)
Next wSheet
No line is highlighted so i cannot determine which is it exactly.
I added watch on Prev_pass and x and it gets the first values then throws up the error.
the returned error is:
Run-time error '1004":
Application-defined or object-defined error
Any help is much appreciated.