Forum Discussion
Matt_C_Paradox
Jan 17, 2023Copper Contributor
Combining Macro for Refreshing Queries across multiple protected sheets
Hi Im new to VBA so really have very little to go on. I found two scripts, one for refreshing queries in protected sheets and another which runs the former across multiple sheets at once i tried to p...
- Jan 17, 2023
Matt_C_Paradox If I understood well, this is what needs to be done.
This bit goes in the ThisWorkbook module:
Private Sub workbook_open() UnprotectAndRefresh End Sub
And this part goes in a normal module (Insert, Module):
Sub UnprotectAndRefresh() Dim xSh As Worksheet Application.ScreenUpdating = False For Each xSh In Worksheets xSh.Unprotect "123" Next End Sub Sub DataRefresh() 'Update by Extendoffice 5/28/2019 ActiveWorkbook.RefreshAll Application.OnTime Now + TimeValue("00:00:01"), "IsRefreshDone" End Sub Sub IsRefreshDone() Dim xSh As Worksheet If Application.CommandBars.GetEnabledMso("RefreshStatus") Then 'Refresh still running, schedule another check in 1 second Application.OnTime Now + TimeValue("00:00:01"), " IsRefreshDone" Else 'Refreshing is done, reprotect sheets For Each xSh In Worksheets xSh.Protect "123" Next End If End Sub
JKPieterse
Jan 17, 2023Silver Contributor
Matt_C_Paradox If I understood well, this is what needs to be done.
This bit goes in the ThisWorkbook module:
Private Sub workbook_open()
UnprotectAndRefresh
End Sub
And this part goes in a normal module (Insert, Module):
Sub UnprotectAndRefresh()
Dim xSh As Worksheet
Application.ScreenUpdating = False
For Each xSh In Worksheets
xSh.Unprotect "123"
Next
End Sub
Sub DataRefresh()
'Update by Extendoffice 5/28/2019
ActiveWorkbook.RefreshAll
Application.OnTime Now + TimeValue("00:00:01"), "IsRefreshDone"
End Sub
Sub IsRefreshDone()
Dim xSh As Worksheet
If Application.CommandBars.GetEnabledMso("RefreshStatus") Then
'Refresh still running, schedule another check in 1 second
Application.OnTime Now + TimeValue("00:00:01"), " IsRefreshDone"
Else
'Refreshing is done, reprotect sheets
For Each xSh In Worksheets
xSh.Protect "123"
Next
End If
End Sub
Matt_C_Paradox
Jan 18, 2023Copper Contributor
Awesome! it works like a dream... I've credited you in a comment at the end 🙂
Am I right in thinking the major change you made was defining the module first then introducing what it does?
Am I right in thinking the major change you made was defining the module first then introducing what it does?
- JKPieterseJan 18, 2023Silver ContributorThat, and making sure each routine is on its own, starting with "Sub RoutineName" and ending with "End Sub", your example code had multiple routines "nested" in each other, that is invalid syntax.