在Excel宏中,我需要取消保护工作表,刷新数据然后保护工作表。以下作品:
Dim rs As Worksheet
For Each rs In Worksheets
rs.Unprotect Password:="SomePassword"
Next rs
ActiveWorkbook.RefreshAll
这不是:
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
产生以下错误:
您尝试更改的单元格或图表受到保护,因此是只读的。
我已尝试过多种方法来延迟宏的继续,直到刷新完成但没有任何效果。我已经看过这个论坛上的方法,但没有找到合适的方法。什么会起作用?
答案 0 :(得分:0)
请尝试以下方法之一:
在DoEvents
之后添加ActiveWorkbook.RefreshAll
。像这样:
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
保护工作簿时将UserInterfaceOnly
参数设置为true。类似的东西:
rs.Protect Password:="SomePassword", UserInterfaceOnly:=True
答案 1 :(得分:0)
我收到与以下子相同的错误消息,我注意到错误消息是指包含数据透视表的工作表:
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