In an Excel macro I need to unprotect a sheet, refresh the data and then protect the sheet. The following works:
Dim rs As Worksheet
For Each rs In Worksheets
rs.Unprotect Password:="SomePassword"
Next rs
ActiveWorkbook.RefreshAll
This does not:
Dim rs As Worksheet
For Each rs In Worksheets
rs.Unprotect Password:="SomePassword"
Next rs
ActiveWorkbook.RefreshAll
For Each rs In Worksheets
rs.Protect Password:="SomePassword"
Next rs
Produces the following error:
The cell or chart you are trying to change is protected and therefore read-only.
I've tried MANY methods to delay the continuing of the macro until the refresh is finished but nothing has worked. I've looked at methods on this forum and found none that work. What will work?
Try one of the following:
Add DoEvents
after ActiveWorkbook.RefreshAll
. Like this:
Dim rs As Worksheet For Each rs In Worksheets rs.Unprotect Password:="SomePassword" Next rs ActiveWorkbook.RefreshAll: DoEvents For Each rs In Worksheets rs.Protect Password:="SomePassword" Next
Set UserInterfaceOnly
argument to true when protecting the workbook. Something like:
rs.Protect Password:="SomePassword", UserInterfaceOnly:=True
I get the same error message with the following sub, I have noticed that the error message refers to worksheets containing pivot tables:
Private Sub Refresh()
Dim wks As Worksheet
For Each wks In ThisWorkbook.Worksheets
wks.Unprotect
Next wks
ThisWorkbook.RefreshAll: DoEvents
For Each wks In ThisWorkbook.Worksheets
wks.Protect UserInterfaceOnly:=True
Next wks
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.