+ Reply to Thread
Results 1 to 14 of 14

protecting sheets with macros using vba

  1. #1
    Registered User
    Join Date
    04-18-2012
    Location
    Gympie, Queensland, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Post protecting sheets with macros using vba

    Hi all,
    I have a workbook with about 50 vba macros in it, and I need it protected from accidental overwrites of formulas.
    I found the code to do this, and it can be seen in the code lines below:

    ActiveSheet.Unprotect Password:="****"
    ActiveSheet.Protect Password:="****", DrawingObjects:=True, Contents:=True, Scenarios:=True

    This works beautifully - it has locked all cells I wanted, runs the macro, is colleague-proof...

    Unfortunately it is also now me-proof.
    I now want to edit some of the locked cells. When I unprotect the sheet manually I can select one spot, then I need to unprotect the sheet again to make a single change. Then unlock again to fill down... it wants to stay locked.

    Is there a way to attach code to a single macro button (or other method) to unlock all cells and leave them unlocked until I make the changes, then relock upon running a macro through another button?

    I don't want to have to remove all the lock / unlock coding in all the macros, make the changes, then put them back.

    All advice gratefully received.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: protecting sheets with macros using vba

    Post the code you use to Lock/Unlock your sheets.

    Did you use this in Event-code such as WorkSheet_Activate, SelectionChange, Change ....
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    04-18-2012
    Location
    Gympie, Queensland, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: protecting sheets with macros using vba

    Sorry it has taken me so long to get back to you - I have had to cut the file down to get it under the limit (even when zipped).

    the lock / unlock code is shown here, and is in every macro.

    Please Login or Register  to view this content.
    regards
    Attached Files Attached Files

  4. #4
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,742

    Re: protecting sheets with macros using vba

    Maybe before each protection you can check who is using sheet and then decide.
    Like:

    Please Login or Register  to view this content.
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  5. #5
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: protecting sheets with macros using vba

    I have a module named 'dev code' that contains the following:

    Please Login or Register  to view this content.

  6. #6
    Forum Expert Roel Jongman's Avatar
    Join Date
    03-28-2015
    Location
    Netherlands
    MS-Off Ver
    Office 365
    Posts
    1,483

    Re: protecting sheets with macros using vba

    The problem is with the code in worksheetmodule sheet1(SUMMATIVE)
    this is an event macro that is handeled each time you move your cursor. that is causing the locks

    Please Login or Register  to view this content.
    the redlines need to be removed from this macro to stop this problem.

    Then I recommend 2 changes.
    1. remove al the protect and unprotect actions from your macro's and create to separate macro's to protect and unproctect the activesheet
    2. In the protect code also include the parameter UserInterfaceOnly:=True what this does is only disallow users to change stuff but macros can run uninterupted without unprotecting them all the time

    By having separate protect and unprotect macro's you can call on them whenever you want. I would not use a button because any body can press that..

    the 2 macro's are very simple and you put them in a Module f.e. Module1 (which is now empty in your sheet)
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Now when you have this you can remove all the unprotect and protect lines from your macro's
    check to make sure your sheets are locked before you remove it.
    the other advantage is that if some sneeky b*sterd discovers your passowrd you only have to change it in one place instead of all macro's

    If you want to be really "fail save" put in each sheet codemodule this event macro

    Please Login or Register  to view this content.
    This then calls the protect macro so then you are sure the sheets are always locked when ppl activate them.

    For your easy use of protecting and unprotecting you could assign shortcut keys to AS_Protect (suggestion CRTL+SHIFT+P) and AS_Unprotect (suggestion CRTL+SHIFT+U)
    this way you can protect and unprotect whenever you want with a few keystrokes
    and with the failsave of locking put in on activation it does not matter much if you forget to put it on.

    So bit of a long story.. but these changes may help you manage protection more easy.

  7. #7
    Registered User
    Join Date
    04-18-2012
    Location
    Gympie, Queensland, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: protecting sheets with macros using vba

    Thank you so much. Works perfectly.

  8. #8
    Registered User
    Join Date
    04-18-2012
    Location
    Gympie, Queensland, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: protecting sheets with macros using vba

    thank you for the assist

  9. #9
    Registered User
    Join Date
    04-18-2012
    Location
    Gympie, Queensland, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: protecting sheets with macros using vba

    working not so perfectly it seems. macro buttons and line colour macro are still not working when sheet is locked. I have added the macros as suggested.
    Attached Files Attached Files

  10. #10
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: protecting sheets with macros using vba

    Place this macro in the code module for ThisWorkbook. Do the following: Hold down the ALT key and press the F11 key. This will open the Visual Basic Editor. In the left hand pane, double click on "ThisWorkbook". Copy/paste the macro into the empty window that opens up. Close the window to return to your sheet. Save the workbook, close it and re-open it. This macro will protect all your sheets but allow your other macros to run. You won't need the two macros to protect/unprotect the sheets. If you want to protect only certain sheets, then the macro will have to be modified.
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  11. #11
    Registered User
    Join Date
    04-18-2012
    Location
    Gympie, Queensland, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: protecting sheets with macros using vba

    Hi Mumps1,

    partial success. The code in sheets 1 and 4 (highlighting rows) will still not work. Similarly, the tabs buttons do change colour successfully but refuse to show/hide the columns.
    I appreciate your assistance
    regards
    Warwick
    Attached Files Attached Files

  12. #12
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: protecting sheets with macros using vba

    Try the attached file. Everything seems to be working for me. Notice the yellow highlighted rows in the four sheets. I removed the Worksheet_SelectionChange macros from the worksheet code modules and placed the macro in the code module for ThisWorkbook.
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    04-18-2012
    Location
    Gympie, Queensland, Australia
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: protecting sheets with macros using vba

    Thank you very much. It is working well.

  14. #14
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: protecting sheets with macros using vba

    You are very welcome.

+ 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. Protecting tabs and protecting sheets
    By jimstrongy in forum Excel General
    Replies: 0
    Last Post: 01-31-2012, 11:27 AM
  2. Protecting sheets with macros
    By faisal.ta in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-25-2010, 06:48 AM
  3. Protecting Cells with Macros
    By sighlent1 in forum Excel General
    Replies: 2
    Last Post: 03-04-2010, 05:00 PM
  4. protecting a workbook but still able to run macros
    By jimb0693 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-09-2009, 11:18 AM
  5. technique:Protecting/Unprotecting sheets with macros
    By eldwardo in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-11-2008, 09:30 PM
  6. Protecting/Unprotecting sheets with macros
    By LAWDAWG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-18-2008, 02:59 PM
  7. [SOLVED] Protecting VBA Macros
    By hecsan07 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-19-2005, 03:05 PM
  8. [SOLVED] Protecting macros
    By Dan E in forum Excel General
    Replies: 4
    Last Post: 01-31-2005, 01:07 AM

Tags for this Thread

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