vba to re-protect sheet after Power Query has finished Refreshing

kcmuppet

Active Member
Joined
Nov 2, 2005
Messages
437
Office Version
  1. 365
Platform
  1. Windows
Hi -

I'm using this code to refresh a query data on a protected sheet . The problem is , the code starts refreshing the query, then re-protects the sheet before the data starts to load to the sheet, and then I sometimes get the warning "The cell...is on a protected sheet...." , and the so the Table doesn't update.

VBA Code:
Sub Refresh_Report()
        
Sheets("Report").Unprotect Password:="123"

ActiveWorkbook.Connections("Query - Full Report").Refresh

Sheets("Report").Protect Password:="123"

End Sub

What do I need to add after to the connection refresh to make Excel wait until the data is loaded to the sheet before re-protecting it?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Maybe

VBA Code:
Application.Wait TimeValue("0:00:10") + Now()
Thanks for your reply. The trouble is, the queries don't take a given amount of time. It's highly variable dependent on the data in the sheets, the pc power, network, etc. I

s there a way to know when the query has finished?
 
Upvote 0
Disable background refresh manually or via code and then refresh:

conn.PNG


VBA Code:
Public Sub Refresh_All()
Dim i%, aw As Workbook
Set aw = ActiveWorkbook
For i = 1 To aw.Connections.Count
    If aw.Connections(i).Type = 1 Then _
    aw.Connections(i).OLEDBConnection.BackgroundQuery = 0
Next
ThisWorkbook.RefreshAll
DoEvents
MsgBox "Done"
End Sub
 
Upvote 0
Disable background refresh manually or via code and then refresh:

View attachment 28375

VBA Code:
Public Sub Refresh_All()
Dim i%, aw As Workbook
Set aw = ActiveWorkbook
For i = 1 To aw.Connections.Count
    If aw.Connections(i).Type = 1 Then _
    aw.Connections(i).OLEDBConnection.BackgroundQuery = 0
Next
ThisWorkbook.RefreshAll
DoEvents
MsgBox "Done"
End Sub
Hi, thanks for your reply.

Am I right in thinking this stops other queries refreshing while this one refreshes? Or does "Type =1" mean in it's this connection?
 
Upvote 0
With this method the queries are refreshed sequentially. Type 1 means an OLE connection, which is what you get for Power Query.
The code below worked for me:

VBA Code:
Public Sub Refresh_All()
Dim i%, aw As Workbook
Set aw = ActiveWorkbook
For i = 1 To aw.Connections.Count
    MsgBox aw.Connections(i).Name & vbLf & aw.Connections(i).Type
    If aw.Connections(i).Type = 1 Then _
    aw.Connections(i).OLEDBConnection.BackgroundQuery = 0
Next
Sheets("Report").Unprotect Password:="123"
ThisWorkbook.RefreshAll
DoEvents
Sheets("Report").Protect Password:="123"
MsgBox "Done"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,957
Latest member
Hat4Life

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