Jan 17 2023 07:13 AM - edited Jan 17 2023 07:16 AM
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 put the code together as below but was met with a error regarding a "rogue end sub"
Private Sub workbook_open()
Sub Dosomething()
Dim xSh As Worksheet
Application.ScreenUpdating = False
For Each xSh In Worksheets
xSh.Select
Call RunCode
Next
Application.ScreenUpdating = True
End Sub
Sub RunCode()
'your code here
Sub DataRefresh()
'Update by Extendoffice 5/28/2019
ActiveSheet.Unprotect "123"
ActiveWorkbook.RefreshAll
Application.OnTime Now + TimeValue("00:00:01"), "DataRefresh2"
End Sub
Sub DataRefresh2()
If Application.CommandBars.GetEnabledMso("RefreshStatus") Then
Application.OnTime Now + TimeValue("00:00:01"), " DataRefresh2"
Else
ActiveSheet.Protect "123"
End If
End Sub
End Sub
I really have no idea what's gone wrong
Jan 17 2023 08:36 AM
Solution@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
Jan 18 2023 01:08 AM
Jan 18 2023 06:45 AM