Forum Discussion
AydeeDiaz
Oct 20, 2022Copper Contributor
Wait until query is refreshed to run the next macro
Hi! I am trying to create a macro, but I need to wait until query is refreshed before doing the copy to paste in the workbook Sub OpenAnyFile() VBA.Shell "Explorer.exe C:\Users\vjrdcx\Aptiv\Apti...
AydeeDiaz
Oct 24, 2022Copper Contributor
Sorry what do you mean??
JKPieterse
Oct 24, 2022Silver Contributor
There is a checkbox in the query properties that either allows or disallows the query to run in the background. Make sure it disallows it.
- AydeeDiazOct 24, 2022Copper ContributorBut the thinks is that I don't have a code to wait until query is refreshed in OTHER workbook and then come back to the code workbook and continue the macro, how can I do it?
- JKPieterseOct 25, 2022Silver Contributor
As long as the queries are set up to be synchronous, the VBA code does not matter as it will wait for the refresh to finish before proceeding.
- AydeeDiazOct 25, 2022Copper ContributorHi, in my case it doesn't wait, is there any code that I can use to make the macro wait until de query in ANTOHER workbook is refreshed? I have tried this:
'____TRY 1
'With ActiveWorkbook.Connections("Query from A2").ODBCConnection.BackgroundQuery = False
'End With
'ActiveWorkbook.Connections("Query from A2").Refresh
'Call CopyData
'____TRY 2
'Dim cn As WorkbookConnection
'For Each cn In ThisWorkbook.Connections
'cn.Ranges(1).ListObject.QueryTable.Refresh BackgroundQuery:=False
'Next cn
'____TRY 3
'Dim dWB As Workbook 'Data Workbook
'Dim FilePath As String
'FilePath = "Explorer.exe C:\Users\vjrdcx\Aptiv\Aptiv Global Business Services - Documents\SPM\Open Items Report\OTC Semantic 2.0 AS&UX 2.xlsm"
'Set dWB = Workbooks(FilePath)
'Dim dWs As Worksheet 'Data Worksheet
'Set dWs = Sheets("AX&US")
'dWB.Activate
'ws.Select
'With dWB.Connections("Query from A2").ODBCConnection.BackgroundQuery = False
'End With
'dWB.Connections("Query from A2").Refresh