Protect Excel Worksheet For Read Only But Enable External Data Refresh

别来无恙 提交于 2019-12-04 07:19:19

Based on Pankaj's suggestion I did the following (although I don't think it's very elegant and still think there must be a better way).

I created a new macro for the workbook.

Sub RefreshData()
'
' RefreshData Macro
'
Application.ScreenUpdating = False
Sheets("sheetname").Unprotect Password:="password"
ActiveWorkbook.Connections("connection name").Refresh
Sheets("sheetname").Protect _
Password:="password", _
UserInterfaceOnly:=True, _
AllowFiltering:=True, _
AllowSorting:=True, _
AllowUsingPivotTables:=True
End Sub

Then I opened up ThisWorkbook in the VBA Project and edited the Workbook Open routine.

Private Sub Workbook_Open()
RefreshData
End Sub

More info about the protection options can be found here: http://datapigtechnologies.com/blog/index.php/worksheet-protection-best-practice/

It works; the sheet is locked everytime the workbook is opened and a refresh of the data is performed. The UserInterfaceOnly property doesn't make a difference to the command to refresh the data (although it should to other macro events). You will still have to specifically unlock the spreadsheet, perform the data refresh and then lock the sheet again.

I added a form button onto one of the other sheets and linked it to my RefreshData macro so that the data can be refreshed manually, while the sheet is supposedly locked.

The other thing I did in the Connection Properties, was to remove the tick against the background refresh.

An easy way to do it is to add a custom button and write a macro. When user presses the toolbar custom button, the macro behind it will unprotect the sheet and refresh the external data and then protect the sheet (with screenupdate set as false obviously)

I would suggest to add the external query on another sheet, not protected, but that you would hide. The data on the protected sheet would simply refer to the unprotected sheet.

I added the data connection to some unprotected sheets, then hid the sheets. Unfortunately the connections then don't work even if the structure of the workbook is protected.

I cant believe I may need to release my code un-protected, there must be an industrial strength solution to this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!