I have a workbook that has connections to SQL and uses Power Query to show the data in the format I need. I have the sheet and workbook protected, but the query will not run while the workbook is protected. So I wrote the simple VBA below, and it properly unprotects/kicks off the refresh/protects, but it protects again so soon that I receive the error that it couldn’t finish. The manual refresh works fine when the workbook is not protected. What is the best way to either have the system wait until the refresh is complete or program in a few seconds of wait time? Unchecking the background option doesn’t work:
Sub UnprotectRefreshProtect()
ThisWorkbook.Unprotect Password:=”abc123″
ThisWorkbook.RefreshAll
ThisWorkbook.Protect Password:=”abc123″, Structure:=True, Windows:=False
End Sub
I tried to uncheck the background refresh in the Query Connection Properties, but that didn’t work. It still protects too soon.
John Buchman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.