I have an existing workbook with 2 visible sheets. The first sheet has a button users can click to update the data on sheet 2 (which is a sharepoint list that updates automatically).
The ActiveWorkbook.RefreshAll method worked initially, but now that my workbook is being distributed outside of it's initial scope I need to be able to protect it (while still allowing users to refresh data)
Since the Activeworkbook.RefreshAll will not work while the target sheet is protected I considered using the following:
Sub RefreshDataSheet
Sheets("Data").Unprotect
ActiveWorkbook.RefreshAll
Sheets("Data").Protect
End Sub
Where "Data" is the name of sheet with the updated list. This attempt still returns the error that "The cell or chart that you are trying to change is protected and therefore read-only."
Does anyone have experience with this? Typically the unprotect then protect strategy works for me - what am I missing?
Thank you in advance.
答案 0 :(得分:0)
RefreshAll
是一个异步命令(这里有更多信息https://msdn.microsoft.com/en-us/library/office/ff838648.aspx)。基本上,它会在调用者完成之前继续运行并将控制返回给调用者。您需要使其在同步模式下工作。
Sub Button1_Click()
'Disable background updates
SetBackgroundConnections False
Sheets("Data").Unprotect
ActiveWorkbook.RefreshAll
Sheets("Data").Protect
'Re-enable background updates
SetBackgroundConnections True
End Sub
Private Sub SetBackgroundConnections(SetOn As Boolean)
Dim cn As WorkbookConnection
For Each cn In ActiveWorkbook.Connections
cn.OLEDBConnection.BackgroundQuery = SetOn
Next
Set cn = Nothing
End Sub