+ Reply to Thread
Results 1 to 14 of 14

Lock AND Protect all work sheets VBA

  1. #1
    Registered User
    Join Date
    10-09-2012
    Location
    waterford, ireland
    MS-Off Ver
    Excel 2007
    Posts
    12

    Lock AND Protect all work sheets VBA

    Hi i keep getting error 1004 when i try this, however it works sometimes and is very random, does exactly what i require but then sometimes the error comes up??

    Would be grateful if anyone can offer any help

    Here is the code anyway

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    08-14-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    573

    Re: Lock AND Protect all work sheets VBA

    Your workbook may not be active when you are running that code.

  3. #3
    Forum Contributor Legend Rubber's Avatar
    Join Date
    09-21-2012
    Location
    Tillsonburg, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    392

    Re: Lock AND Protect all work sheets VBA

    Add
    Please Login or Register  to view this content.
    after your Dim wsworsheet.. to avoid that problem...
    Last edited by Legend Rubber; 12-04-2012 at 12:21 PM. Reason: added point

  4. #4
    Registered User
    Join Date
    10-09-2012
    Location
    waterford, ireland
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Lock AND Protect all work sheets VBA

    Hi thanks for the responses, Sinhus - this macro works perfectly with different code like just locking all the sheets. Should this code not go through each worksheet??

    Hi Legend Rubber, unfortuantely that wouldnt work as this macro needs to be used across loads of files and all tabs will have different names.

    What i am essentially looking for is a macro to lock everything so its like a hard copy for audit purposes for example. Nobody could potentially edit it

  5. #5
    Forum Contributor Legend Rubber's Avatar
    Join Date
    09-21-2012
    Location
    Tillsonburg, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    392

    Re: Lock AND Protect all work sheets VBA

    okay, what about
    Please Login or Register  to view this content.
    ?

  6. #6
    Valued Forum Contributor
    Join Date
    08-14-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    573

    Re: Lock AND Protect all work sheets VBA

    Looping is fine. Only thing is your workbook must be active, before you execute the code For each sheet in Activeworkbook. If none of the workbook is active at the time of execution, it will throw error. That is why you are getting that error randomly.
    Quote Originally Posted by rduggy View Post
    Hi thanks for the responses, Sinhus - this macro works perfectly with different code like just locking all the sheets. Should this code not go through each worksheet??

    Hi Legend Rubber, unfortuantely that wouldnt work as this macro needs to be used across loads of files and all tabs will have different names.

    What i am essentially looking for is a macro to lock everything so its like a hard copy for audit purposes for example. Nobody could potentially edit it

  7. #7
    Forum Contributor Legend Rubber's Avatar
    Join Date
    09-21-2012
    Location
    Tillsonburg, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    392

    Re: Lock AND Protect all work sheets VBA

    Sindhus,
    just for my knowledge, would the ((workbooks("yourworkbookname").activate )) code that i suggested work to avoid this error?
    or is there a better way?

  8. #8
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Lock AND Protect all work sheets VBA

    Hi -

    Is the line
    Please Login or Register  to view this content.
    get highlighted after the error? if so, then your workbook has a hidden sheets which causing the error.

    Regards,
    Event

  9. #9
    Valued Forum Contributor
    Join Date
    08-14-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    573

    Re: Lock AND Protect all work sheets VBA

    I think this is only part of the code. OP said in the previous post that this code will be executed for loads of files. so it may not be desirable to manually type the file name everytime.

    @rduggy How are you executing this code for loads of files? Are you using separate code for that?
    Quote Originally Posted by Legend Rubber View Post
    Sindhus,
    just for my knowledge, would the ((workbooks("yourworkbookname").activate )) code that i suggested work to avoid this error?
    or is there a better way?

  10. #10
    Registered User
    Join Date
    10-09-2012
    Location
    waterford, ireland
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Lock AND Protect all work sheets VBA

    Hi everyone, i tried legend Rubber suggestion of

    Please Login or Register  to view this content.
    But no luck

    @event21 yeah that is exactly the issue except i have no hidden tabs and the same error pops up even if i open a new excel file and add the macro.

    Would be very grateful if someone had a solution.

    Has anyone tried this themselves??

  11. #11
    Forum Contributor Legend Rubber's Avatar
    Join Date
    09-21-2012
    Location
    Tillsonburg, Ontario
    MS-Off Ver
    Excel 2010
    Posts
    392

    Re: Lock AND Protect all work sheets VBA

    why dont you post a workbook with the code so we can play around with it...
    Sorry that my suggestion didnt work... we will try again

  12. #12
    Valued Forum Contributor
    Join Date
    08-14-2012
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    573

    Re: Lock AND Protect all work sheets VBA

    Try this one. I got an error when protecting sheet2. Cells.select was pointing to sheet1 only. So i selected sheet2 before executing cells.select

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    10-09-2012
    Location
    waterford, ireland
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Lock AND Protect all work sheets VBA

    Hi Sindhus didnt see you post till now, just tested and it seems to have worked

    Makes sense too lol

    Thanks again

  14. #14
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Lock AND Protect all work sheets VBA

    for info there is no need to select or activate
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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