VBA to unlock worksheet when clicking on "refresh all" ribbon button so it can refresh a power query source

Joined
Aug 3, 2021
Messages
14
Office Version
  1. 2016
Platform
  1. Windows
I'm looking for a way to use the "refresh all" ribbon button to update my power query that's in a protected sheet. I found code that works when opening the workbook, but am getting an error when trying to refresh all in the protected sheet.

The worksheet name is "Daily Activity"
The query name in the protected sheet is "daily_activity_detailed_AEI_all"

Goal: When pressing "refresh all" button in ribbon under the data tab:
  • Daily Activity sheet is unprotected
  • All connections are refreshed
  • Daily Activity sheet is protected again
I keep getting error stating sheet is protected and query can't be refreshed without unprotecting it.

Current VBA code (in workbook module) for both the "refresh all" and for when I open the workbook (the open workbook is working correctly):

VBA Code:
Option Explicit
Private WithEvents QT As QueryTable

Private Sub QT_AfterRefresh(ByVal Success As Boolean)
    If Success Then
    Sheets("Daily Activity").Unprotect
    ActiveWorkbook.Connections("Query - daily_activity_detailed_AEI_all").Refresh

    End If
End Sub

Private Sub Workbook_Open()
    Sheets("Daily Activity").Unprotect
    ActiveWorkbook.Connections("Query - daily_activity_detailed_AEI_all").Refresh
    Sheets("Daily Activity").Protect
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Well, if your code works well when opening a workbook, just place it in your Personal Workbook and assign a new custom button to it somewhere in the Ribbon.

For example:
VBA Code:
Public Sub RefreshAllQueries()
    Sheets("Daily Activity").Unprotect
    ActiveWorkbook.Connections("Query - daily_activity_detailed_AEI_all").Refresh
    Sheets("Daily Activity").Protect
End Sub

As far as I know the built-in buttons cannot be modified, so you'll ned a custom button.

If you don't know how to do it, here's an example:

1707690869591.png


1707690953960.png

1707691145790.png
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,155
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top