+ Reply to Thread
Results 1 to 19 of 19

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

Hybrid View

  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!


    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim ws As Worksheet
    
    If Target.Address = "$C$3" Then
    
     For Each ws In ThisWorkbook.Worksheets
       ws.Unprotect Password:="xxx"
     Next ws
    
    Application.Wait (Now + TimeValue("0:00:10"))
    
     Workbooks(ThisWorkbook.Name).RefreshAll
     
     
     For Each ws In ThisWorkbook.Worksheets
       ws.Protect Password:="xxx"
     Next ws
    
    End If
    End Sub

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

    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, 365
    Posts
    8,056

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

    I disabled these two lines of code:
    Application.Wait (Now + TimeValue("0:00:10"))
     Workbooks(ThisWorkbook.Name).RefreshAll
    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, 365
    Posts
    8,056

    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

    I unprotected sheet
    Then commented out the unprotect / protect lines

    .... it worked!

    *scratching my head*

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

    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?

  9. #9
    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!

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

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

    Use
    Sheets("Sheet1").Unprotect Password:="xyx"
    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.

  11. #11
    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!

  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 Do Events I tried
    If Application.Ready = True Then
    continue to Protect sheets

    But that didn't work either

  13. #13
    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

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim ws As Worksheet
    Dim lo As ListObject
    
    
    If Target.Address = "$D$2" Then
    
    Application.ScreenUpdating = False
    
     For Each ws In ThisWorkbook.Worksheets
       ws.Unprotect Password:="xx"
    
           For Each lo In ws.ListObjects
            lo.QueryTable.Refresh BackgroundQuery:=False
        Next lo
    
     Next ws
     End If
    
     For Each ws In ThisWorkbook.Worksheets
       ws.Protect Password:="xx"
     Next ws
    
    Application.ScreenUpdating = True
    End Sub
    Love code .... sometimes!!

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

    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