Runtime error 57121

H

Henk

To unprotect my entire workbook on opening, I start on ThisWorkbook the
following procedure :

Private Sub WorkBook_Open()
Application.ScreenUpdating = False

For Each WkSht in Worksheets
WkSht.Unprotect Password:="mypassword"
Next WkSht

I have a lot of workbooks with this procedure fuctioning properly, however
there is one which each time results in Runtime error 57121 :
Applikcation-defined or object-defined error. Debugging points me to the line
: WkSht.Unprotect Password:="mypassword"

(In the same workbook I have a macro, using the same code, wich is working
without errors.)

Any idea how to solve this?
 
D

Dave Peterson

Try qualifying the worksheets:

For Each WkSht in Worksheets
becomes:
For Each WkSht in Me.Worksheets
 
H

Henk

Dave,

Thanks for you suggestion. I tried it, but it did not work.

I found out where the problem comes from. I have a UserForm with Listbox
that is used within several sheets. I used the "ControlSource" of the Listbox
as a place to store the choice of the user, which worked perfectly by the
way, but probably this is not the way to use the ControlSource. A soon as I
remove the UserForm, my OpenWorkbook() is functioning properly.

I am working on a way to make it work with UserForm.ListBox.ListIndex

tHenks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top