Macro to refresh PQ

bobaol

Board Regular
Joined
Jun 3, 2002
Messages
225
Office Version
  1. 365
  2. 2003 or older
Platform
  1. Windows
Hello, I am using a macro to refresh the PowerQuery connected to a MS SQL Server. My question, how do I make sure the data is refreshed before continuing to the next step? Depending on the size of my dataset, it may take very little time, or it may take a few seconds. I want to make sure all the data is refreshed before continuing to the next step of copy-past as values, and do other stuff. any help is appreciated. thanks in advance. here's my code:
VBA Code:
Sub Macro41____refresh_tables_()
'refresh data
    ActiveWorkbook.Connections("Query - Cohort").Refresh
    ActiveWorkbook.Connections("Query - vwScreeningPatientList").Refresh

'''select all, paste as values
    Application.Goto Reference:="R1C1"
    ActiveCell.Columns("A:AZ").EntireColumn.Select
    Selection.Copy
    Sheets("Values").Select
    Application.Goto Reference:="R1C1"
    Selection.PasteSpecial Paste:=xlPasteValues
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I recall (from a long time ago) this is an issue. My memory is to wait for an object linked to the query to refresh rather than the query itself. Eg, have the query refresh a table in the worksheet and then use VBA to wait for that event. I don’t remember the code.
 
Upvote 0
You should be able to turn off the background refresh property of the connections. Then they should wait to complete before moving on to the next line.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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