Forum Discussion

Matt_C_Paradox's avatar
Matt_C_Paradox
Copper Contributor
Jan 17, 2023
Solved

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...
  • JKPieterse's avatar
    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
    
    

Resources