+ Reply to Thread
Results 1 to 9 of 9

Protect and unprotect a worksheet in VBA

  1. #1
    Registered User
    Join Date
    05-20-2013
    Location
    Kegworth, England
    MS-Off Ver
    Excel 2019
    Posts
    58

    Protect and unprotect a worksheet in VBA

    Hi all,
    I have a WorkBook consisting of 9 Worksheets. The administrator is the main user of the WorkBook and has access to all the sheets.
    All sheets are accessed by command buttons on sheet1, by either hyperlinks or by, in this case, an assigned macro.
    On sheets 3, 5 and 7 however, between rows 82 to 203, there is sensitive employee pay rate data, for which I only want the Manager to have access to.
    When the administrator opens say, sheet 3 (5 and 7 are similar but different departments) a macro runs to hide the sensitive rows then protects the sheet with the password (apricot). The macro to do this works fine. So far so good.
    My problem is, when accessing the sensitive data. By using the code below it all works perfectly fine as long as the correct password is entered.
    If a wrong password is entered, or cancel is pressed then 'run time error 1004' box appears.
    How can I either go back to Sheet1 when 'cancel' is pressed, or continue to ask for a password if the wrong one is entered?

    It is probably quite simple but for the life of me i cannot work it out.

    Any help gratefully received

    Andrew


    Please Login or Register  to view this content.

  2. #2
    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: Protect and unprotect a worksheet in VBA

    Hi,

    Perhaps this
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Protect and unprotect a worksheet in VBA

    In my experience, sensitive (payroll) data is easier to safeguard in separate sheet(s) that (in your case) only the manager has access to.
    It keeps everything simpler.
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  4. #4
    Registered User
    Join Date
    05-20-2013
    Location
    Kegworth, England
    MS-Off Ver
    Excel 2019
    Posts
    58

    Re: Protect and unprotect a worksheet in VBA

    Thanks xlnitwit for your reply,
    That works to a certain extent in that when a 'cancel is pressed the vba dialog box appears with run-time error: '9' displayed. pressing 'end' takes me back to Sheet1.
    Inputting the wrong password again brings up the vba dialog box, this time with run-time error: '-2147352565 (8002000b)'.
    Pressing 'debug', in both cases, takes me back to the line after 'Else' -Sheets("Sheet1").select

    Any clues as to why it does this?

  5. #5
    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: Protect and unprotect a worksheet in VBA

    The error 9 would suggest that you do not have a sheet called Sheet1 in the workbook.

  6. #6
    Registered User
    Join Date
    05-20-2013
    Location
    Kegworth, England
    MS-Off Ver
    Excel 2019
    Posts
    58

    Re: Protect and unprotect a worksheet in VBA

    Do you know kev, when it became apparent to me that people other than just me would be using the workbook, that's when I realised that putting the data on seperate sheets was the way to go.
    I suppose I'm just trying to learn a bit of vba to do the trick - if it could.
    I might just have to bite the bullet and separate the data into seperate sheets.

  7. #7
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Protect and unprotect a worksheet in VBA

    You can do it by hiding rows and it will work perfectly well
    - but in the long term it's more painful
    - every time you modify the worksheet you have to remember to modify the hidden rows in VBA
    (it's Friday at 4.55pm, you add a sensitive row... time to go home and then forget to alter the VBA on Monday morning...
    - it's more risky to have mixed sheets

  8. #8
    Registered User
    Join Date
    05-20-2013
    Location
    Kegworth, England
    MS-Off Ver
    Excel 2019
    Posts
    58

    Re: Protect and unprotect a worksheet in VBA

    What a plonker!
    I should have put 'Sheets("Interface").Select'
    I've changed it and all works perfectly. Thanks

  9. #9
    Registered User
    Join Date
    05-20-2013
    Location
    Kegworth, England
    MS-Off Ver
    Excel 2019
    Posts
    58

    Re: Protect and unprotect a worksheet in VBA

    You're absolutely right of course!
    Thanks for the advice,

+ 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. VBA Protect/Unprotect worksheet
    By DDBU in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-03-2013, 05:19 AM
  2. [SOLVED] Trying to use VB to protect and Unprotect a worksheet
    By jfoerch in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-09-2013, 09:44 AM
  3. Protect & Unprotect Worksheet
    By cyee in forum Excel General
    Replies: 5
    Last Post: 01-16-2012, 04:53 AM
  4. Protect and Unprotect Worksheet VBA
    By s45yth in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-11-2011, 07:20 PM
  5. Protect/Unprotect worksheet
    By mab in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-16-2010, 06:21 PM
  6. SQL to unprotect and protect worksheet
    By drewship in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-07-2009, 09:32 AM
  7. How to protect and unprotect worksheet
    By ballack in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-21-2007, 08:35 AM
  8. [SOLVED] Protect/Unprotect Worksheet
    By djn in forum Excel General
    Replies: 2
    Last Post: 05-12-2005, 07:06 PM

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