+ Reply to Thread
Results 1 to 19 of 19

VBA to unprotect sheets x 2, refresh data then protect sheets

  1. #1
    Forum Contributor
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    125

    VBA to unprotect sheets x 2, refresh data then protect sheets

    Hi I have this code that will run when something entered into C3.

    I can't get it to run properly as I get error saying the sheet it still locked
    If I step through the code (F8) it works but not if I let it run
    Your advice will be much appreciated!


    Please Login or Register  to view this content.

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

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    Is cell C3 unlocked?
    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.

  3. #3
    Forum Contributor
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    125

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    Yes it is
    If I step through code it works - why wont it work if it is left to run

    I thought that adding the 10sec delay will give whatever in background to work before refreshing data but it seems that not it

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

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    I disabled these two lines of code:
    Please Login or Register  to view this content.
    and tried it in a dummy file and it worked for me. Can you attach a copy of your file. De-sensitize it if necessary.

  5. #5
    Forum Contributor
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    125

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    I need the RefreshAll line in as I want data to Refresh before protecting the sheet again

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

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    I just disabled those lines because I had no data in my dummy file. The 'Unprotect' and 'Protect' worked properly without an error message.

  7. #7
    Forum Contributor
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    125

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    In mine I have external data that is refreshed when the value of the cell changes

    Any suggestions on modifying my code to make it work

    Thank you for you time on this !

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

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    Just as a test, try leaving the sheet unprotected and remove the 'Unprotect' and 'Protect' lines from the macro? See if that works for you.

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

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    Sorry...try with all sheets unprotected.

  10. #10
    Forum Contributor
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    125

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    I unprotected sheet
    Then commented out the unprotect / protect lines

    .... it worked!

    *scratching my head*

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

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    Me too. Could I suggest you start a new thread explaining the problem .... it works without the protection but not with the protection?

  12. #12
    Forum Contributor
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    125

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    Instead of having a loop to unprotect how may I say like ...

    Sheet(0).Unprotect password = xyx
    Sheet(1).Unprotect password = xyx

    THanks!

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

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    Use
    Please Login or Register  to view this content.
    Replace Sheet 1 with the sheet name. You will have to repeat this line for each worksheet. I don't think the looping is the problem, however.

  14. #14
    Forum Contributor
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    125

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    No you are right BOTH ways I get the same error

    It's like the RefreshAll is running before the Sheets are unprotected .... weird!

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

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    Try this line:
    Please Login or Register  to view this content.
    in stead of
    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    125

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    Hey Mumps

    Guess what ... it didn't work
    Have now figured out that the RefreshAll doesn't get enough time to run, the sheets are then Protected again ...hence getting the error.
    Seems that 'DoEvents' is not working

    Code is fun !

    Thanks so much for your help so far!

  17. #17
    Forum Contributor
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    125

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    Instead of Do Events I tried
    Please Login or Register  to view this content.
    continue to Protect sheets

    But that didn't work either

  18. #18
    Forum Contributor
    Join Date
    10-29-2009
    Location
    London
    MS-Off Ver
    Excel 2013
    Posts
    125

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    I finally got it to work

    Please Login or Register  to view this content.
    Love code .... sometimes!!

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

    Re: VBA to unprotect sheets x 2, refresh data then protect sheets

    Glad it worked out.

+ 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] Protect/Unprotect sheets
    By Lukael in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-17-2014, 07:33 PM
  2. [SOLVED] Macro to Unprotect sheets-unlock range of cells-protect sheets
    By jrace in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-09-2013, 10:45 AM
  3. Unprotect the multiple sheets and protect them again
    By itisalikhan in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-08-2012, 01:12 PM
  4. [SOLVED] protect/unprotect all sheets
    By wayneg in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-03-2012, 08:20 AM
  5. [SOLVED] Password protect and unprotect all sheets EXCEPT two named sheets
    By Ducatisto in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-29-2012, 05:43 AM
  6. Protect/Unprotect all sheets in a different file
    By skycowboy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-04-2011, 01:30 PM
  7. protect/unprotect all sheets
    By roos in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-13-2006, 06:50 PM
  8. Protect/Unprotect sheets (without activating them)
    By Te4t0n in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-12-2005, 03:47 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