VBA Wait for Query Refresh

%3CLINGO-SUB%20id%3D%22lingo-sub-2267041%22%20slang%3D%22en-US%22%3EVBA%20Wait%20for%20Query%20Refresh%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2267041%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Excel%20Community%2C%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20having%20issues%20with%20a%20detailed%20macro%20that%20requires%20multiple%20queries%20to%20be%20refreshed%20several%20times%20throughout%20the%20running%20of%20the%20VBA%20code%20to%20allow%20the%20user%20to%20visualise%20updates%20that%20are%20being%20sent%20to%20the%20company%20SQL%20database.%20The%20issue%20occurs%20where%20the%20code%20continues%20to%20run%20even%20while%20the%20queries%20are%20still%20refreshing%2C%20meaning%20that%20the%20code%20is%20completing%20calculations%20within%20the%20workbook%20based%20on%20the%20old%20data%20within%20the%20query%20tables%20rather%20than%20the%20new%20data%20that%20was%20just%20sent%20to%20the%20SQL%20database%20through%20previous%20steps%20of%20the%20same%20macro.%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20done%20a%20fair%20bit%20of%20trialling%20based%20on%20other%20discussions%20and%20forums%20around%20the%20same%20issue%20but%20none%20have%20worked%20so%20far%20(i.e.%20Query%20Background%20Refresh%20is%20greyed%20out%2Funticked%20already%2C%20trialled%20using%26nbsp%3B%3C%2FP%3E%3CDIV%3EApplication.CalculateUntilAsyncQueriesDone%2C%20inserting%20a%20pause%2Fwait%20action)%26nbsp%3B%3C%2FDIV%3E%3CDIV%3ENote%20that%20there%20also%20seems%20to%20be%20an%20issue%20where%20query%20refresh%20speeds%20differ%20for%20users%20depending%20on%20which%20excel%20version%20they%20have.%20The%20issue%20was%20not%20obvious%20during%20trialling%20on%20my%20Windows%20device%20as%20I%20have%20the%20latest%20MS%20Excel%2C%20but%20for%20users%20with%20slightly%20older%20accounts%20the%20code%20encounters%20an%20error%20due%20to%20serious%20hold%20ups%20from%20the%20query%20refresh.%26nbsp%3B%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EUltimately%20what%20I%20am%20looking%20for%2C%20is%20to%20be%20able%20to%20identify%20within%20the%20code%20that%20the%20refresh%20of%20either%20a%20specific%20query%20or%20all%20queries%20has%20in%20fact%20%3CEM%3E%3CSTRONG%3Ecompleted%3C%2FSTRONG%3E%20%3C%2FEM%3Ea%20refresh%20before%20continuing%20with%20calculations%20immediately%20after.%20Is%20there%20something%20that%20can%20call%20the%20exact%20time%20that%20a%20query%20refresh%20was%20completed%3F%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2267041%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Visitor

Hi Excel Community, 

I am having issues with a detailed macro that requires multiple queries to be refreshed several times throughout the running of the VBA code to allow the user to visualise updates that are being sent to the company SQL database. The issue occurs where the code continues to run even while the queries are still refreshing, meaning that the code is completing calculations within the workbook based on the old data within the query tables rather than the new data that was just sent to the SQL database through previous steps of the same macro. 

I have done a fair bit of trialling based on other discussions and forums around the same issue but none have worked so far (i.e. Query Background Refresh is greyed out/unticked already, trialled using 

Application.CalculateUntilAsyncQueriesDone, inserting a pause/wait action) 
Note that there also seems to be an issue where query refresh speeds differ for users depending on which excel version they have. The issue was not obvious during trialling on my Windows device as I have the latest MS Excel, but for users with slightly older accounts the code encounters an error due to serious hold ups from the query refresh. 
 
Ultimately what I am looking for, is to be able to identify within the code that the refresh of either a specific query or all queries has in fact completed a refresh before continuing with calculations immediately after. Is there something that can call the exact time that a query refresh was completed?

 

1 Reply
Impossible to answer without seeing at least some relevant bits of your code I'm afraid.
You don't mention what type of query you are talking about, e.g. is this through the legacy query wizard, PowerQuery, VBA, some other connection? A QueryTable object has a Refreshing property which should tell you if it is -well- refreshing.