+ Reply to Thread
Results 1 to 7 of 7

VBA code to protect all worksheets with password when workbook is opened

  1. #1
    Registered User
    Join Date
    11-15-2013
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    25

    VBA code to protect all worksheets with password when workbook is opened

    Hi,

    I am using the following code to protect all the worksheets in my workbook when the workbook is opened.

    Please Login or Register  to view this content.
    It works great, but only if I stick to the same password. If I try to change the password I get a Run-time error '1004' saying the password I supplied is not correct. However, the new password which I've added to the code does work when I manually unprotect the sheets with the Unprotect Sheet function. I've tried deleting the code completey, unprotecting each worksheet, then saving and reopening and adding the code back and I still get the error.

    Does anyone know what might be happening or how to get rid of the Run-time error?

    Please let me know.

    Best,

    Andrej

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA code to protect all worksheets with password when workbook is opened

    Stab in the dark here - but you have Unprotected it using the OLD password before trying to add the new one...?

  3. #3
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: VBA code to protect all worksheets with password when workbook is opened

    .
    .
    The following works fine here. Very similar to your macro.

    #1 Once opening the workbook, individually unprotect all sheets with the old password.

    #2 Change the password in the macro to something different.

    #3 Go back through the workbook and manually change the password for each sheet.

    #4 Then save and close.

    When reopening the workbook, the changed password worked ... here.

    ????


    Please Login or Register  to view this content.

    I believe this is the same thing cytop is saying ??

  4. #4
    Registered User
    Join Date
    11-15-2013
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: VBA code to protect all worksheets with password when workbook is opened

    Hi,

    I have tried both suggestions. I unprotected all the sheets with the old password, then replaced the password in the VBA code, then re-locked all the sheets with the new password and then saved, closed, and re-opened and still got the error.

    When I tried the suggested code there is no error message but it does not lock the worksheets when the workbook is opened. I put the code in the ThisWorkbook object, is this correct?

  5. #5
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: VBA code to protect all worksheets with password when workbook is opened

    .
    .
    The macro code would go into a Routine Module, not in the ThisWorkbook module.

    The code that should be in ThisWorkbook module would be :

    Please Login or Register  to view this content.

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA code to protect all worksheets with password when workbook is opened

    Hi,

    Are you sure you don't have any hidden sheets in the workbook, and that the password exactly matched the one you entered manually?
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  7. #7
    Registered User
    Join Date
    11-15-2013
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: VBA code to protect all worksheets with password when workbook is opened

    It was a hidden sheet I forgot about! My original code works fine now. Thanks everyone for the help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Unprotect & Protect All workbook sheets password protection / existing code help needed
    By annazet in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-20-2017, 04:51 PM
  2. Replies: 2
    Last Post: 11-17-2014, 04:28 PM
  3. [SOLVED] password protect worksheets upon opening or saving a workbook.
    By emilyloz in forum Excel General
    Replies: 2
    Last Post: 06-14-2013, 03:14 PM
  4. [SOLVED] EXCEL VBA USERFORM- PASSWORD PROTECTION for worksheets, workbook and code
    By PANTECH in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-23-2013, 03:19 PM
  5. Replies: 0
    Last Post: 03-12-2013, 02:33 AM
  6. Protect Sheet & Password Protect Code
    By KLahvic in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-13-2009, 04:08 PM
  7. Replies: 2
    Last Post: 07-16-2005, 11:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1