• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Pause macro until Power Queries finished refreshing|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Pause macro until Power Queries finished refreshing|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosPause macro until Power Queries fin…
sp_PrintTopic sp_TopicIcon
Pause macro until Power Queries finished refreshing
Avatar
Blanka Blair
Member
Members
Level 0
Forum Posts: 53
Member Since:
October 17, 2015
sp_UserOfflineSmall Offline
1
March 24, 2017 - 6:16 am
sp_Permalink sp_Print

Hi All,

I have three tables created by Power Query in my file.

Is there a way to have a macro refresh the queries, ideally one at a time? And the macro would need to pause performing other tasks until the queries are finished refreshing.

I found something on the web, but it pertained to pivot tables and I am not using them in my file.

Thank you,

Blanka

sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
March 24, 2017 - 4:07 pm
sp_Permalink sp_Print

Hi Blanka,

You should read this topic, it's a similar discussion.

Avatar
Blanka Blair
Member
Members
Level 0
Forum Posts: 53
Member Since:
October 17, 2015
sp_UserOfflineSmall Offline
3
March 25, 2017 - 1:11 am
sp_Permalink sp_Print

Hi Catalin,

Thank you for responding.

I clicked on the link you provided, but it comes up empty. I wonder if it's part of the forums reserved to people who are currently subscribed to Power Query training. My subscription already expired.

I would be very grateful for any help.

Thank you,

Blanka

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
March 25, 2017 - 4:51 am
sp_Permalink sp_Print sp_EditHistory

Indeed, it's in the Power Quwry forum for members.

You have 2 options to refresh a connection: with background refresh set to false or to true.
Go to Data-Connections, select the connection you want, click on Properties, and uncheck the checkbox "Enable background refresh"
This way, the next line will not be executed until the ​refresh is complete.

If you have many connections, use a simple code to disable that option:

 

Sub ChangeConnectionRefreshMode()
Dim Connection As Variant
For Each Connection In ActiveWorkbook.Connections
    Connection.OLEDBConnection.BackgroundQuery = False
Next Connection
End Sub

You will get such code if you use the macro recorder while performing this action manually for a connection. Unfortunately, there is a bug, the code will continue before the last query is completed, here is Gregory's workaround:

Gregory Regan said
I have the solution! Crude but should work...

When refreshing the queries programmatically, they seem to process in alphanumeric order based on the name of the queries. Therefore, create a permanent hidden dummy WorkSheet with a single row of data and then create a simple query on it and name the query "ZZZZZZZZZZZ_DummyQry" or something similar.

When the code loops through the queries, the "ZZZZZZZZZZZ_DummyQry" query will run last (and finish in a fraction of a second) ensuring that last genuine query is finished processing before moving onto the code to protect all worksheets because of the code line Connection.OLEDBConnection.BackgroundQuery = False.  

sp_AnswersTopicAnswer
Answers Post
Avatar
Blanka Blair
Member
Members
Level 0
Forum Posts: 53
Member Since:
October 17, 2015
sp_UserOfflineSmall Offline
5
March 25, 2017 - 8:21 am
sp_Permalink sp_Print

Thank you so much, Catalin.

To double check - I created a dummy ZZZZ query and used your code and entered the command to refresh all my connections in the following place:

Sub Convert()

Dim Connection As Variant
For Each Connection In ActiveWorkbook.Connections
    Connection.OLEDBConnection.BackgroundQuery = False

    Connection.Refresh

Next Connection

Call CUSI
DoEvents

Call CUA
DoEvents

Call CUSH
DoEvents

End Sub

 

Is this correct?

I'll have to do some testing after the weekend, but I think it might work how I need it.

Thank you again.

Blanka

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
6
March 25, 2017 - 2:12 pm
sp_Permalink sp_Print

Yes, should work.

Keep in mind that you don't have to set the BackgroundQuery to false in this code, it's enough f you do it manually, or just run it once to change that setting.

Avatar
Blanka Blair
Member
Members
Level 0
Forum Posts: 53
Member Since:
October 17, 2015
sp_UserOfflineSmall Offline
7
March 28, 2017 - 12:58 am
sp_Permalink sp_Print

Many thanks, Catalin!

Avatar
Darshan jain
Santa Clara

New Member
Members
Level 0
Forum Posts: 2
Member Since:
October 26, 2017
sp_UserOfflineSmall Offline
8
December 22, 2017 - 7:51 pm
sp_Permalink sp_Print

It's not working, i had just copy paste in VBA, still it's not working, can you please let me know how to do?

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
9
December 23, 2017 - 12:03 am
sp_Permalink sp_Print

Hi Darshan,

You will have to provide more details, screenshots there is no clue about what you are doing and what does not work.

Thank you for understanding.

It's better if you open your own topic instead of writing in an older topic.

Avatar
Andrew K

New Member
Members
Level 0
Forum Posts: 1
Member Since:
January 13, 2019
sp_UserOfflineSmall Offline
10
January 13, 2019 - 8:06 pm
sp_Permalink sp_Print

Catalin Bombea said 

You will get such code if you use the macro recorder while performing this action manually for a connection. Unfortunately, there is a bug, the code will continue before the last query is completed 

Just hit this issue myself.
If the bug is still an issue, another (untested) workaround is to loop twice.
This may still be an issue with a single connection.

Sub ChangeConnectionBackgroundRefreshMode()
    'On Error Resume Next
    Dim conn As WorkbookConnection
    Dim bugfix As Integer
    For bugfix = 1 To 2
        For Each conn In ActiveWorkbook.Connections
            With conn
                If (.Type = xlConnectionTypeOLEDB) Then
                    .OLEDBConnection.BackgroundQuery = false
                Else
                If (.Type = xlConnectionTypeODBC) Then
                    .ODBCConnection.BackgroundQuery = false
                End If
                End If
            End With
        Next conn
    Next bugfix
End Sub

Thanks,
Andrew K.

Avatar
chris hartnell

Active Member
Members
Level 0
Forum Posts: 5
Member Since:
October 11, 2019
sp_UserOfflineSmall Offline
11
October 11, 2019 - 11:52 pm
sp_Permalink sp_Print

I cannot get the solutions above to work for me. I have created a simple set of 3 queries in the workbook, the first two take about 5 seconds to refresh, the third is one row query called ZZZZ. The code below still jumps to the MsgBox line.

Any suggestions much appreciated. I need to run a protect wb code in my real work situation but the macro falls over because the Query is still refreshing and trying to load up the table after the wb is locked again.

 

Option Explicit

Sub refreshQ()

    Dim conn As WorkbookConnection

    Dim bugfix As Integer

   

    For bugfix = 1 To 2

        For Each conn In ActiveWorkbook.Connections

            With conn

                If (.Type = xlConnectionTypeOLEDB) Then

                    .OLEDBConnection.BackgroundQuery = False

                Else

                    If (.Type = xlConnectionTypeODBC) Then

                        .ODBCConnection.BackgroundQuery = False

                    End If

                End If

            End With

        Next conn

    Next bugfix

           

    For Each conn In ActiveWorkbook.Connections

       conn.Refresh

    Next conn

    MsgBox "Hello"

       

End Sub

Avatar
Max Rottersman

New Member
Members
Level 0
Forum Posts: 2
Member Since:
December 9, 2020
sp_UserOfflineSmall Offline
12
December 9, 2020 - 1:07 am
sp_Permalink sp_Print sp_EditHistory

Why is this still a problem 3 years later!  Shesh Microsoft!  Anyway, THANKS to Gregory.

Step 1:  I created a "hello world" query from a blank query and a sheet to store result

Step 2: Refresh query before my other queries seems to do the trick.  (BTW, I have a trick to pull in only "pages" from PDFs if anyone interested).

' ONLY RUNNING THIS TO PREVENT "UNEXPECTED ERROR" Silly frown POPUP ' It forces Power Query to finish up last query before VBA code asks to do another in a loop

Sheets("DummyQueryForKludgeWait").Select Columns("A:A").

Select Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False

Started getting problems with above, so doing this now

' ONLY RUNNING THIS TO PREVENT UNEXPECTED ERROR POPUP
' it forces Power Query to finish up before VBA code asks for more

Sheets("DummyQueryForKludgeWait").Select
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=DummyQueryForKludgeWait;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [DummyQueryForKludgeWait]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "DummyQueryForKludgeWait"
.Refresh BackgroundQuery:=False
End With
' Now delete link to query, I wish I understood better!
Columns("A:A").Select
Selection.ListObject.QueryTable.Delete
Selection.ClearContents
Range("A1").Select

Avatar
Paul Aud

New Member
Members
Level 0
Forum Posts: 1
Member Since:
August 30, 2022
sp_UserOfflineSmall Offline
13
August 31, 2022 - 12:05 am
sp_Permalink sp_Print

I also set background refresh to off, then I use the following code to specify the order of refresh:rnDim wb as workbookrnSet wb = ThisWorkbookrnwb.Connections("Query - [your query name here]").RefreshrnI still had some problems with queries not seeming to be finished before others started, so I added a 1 second delay in between using a sub I call "wait"rnSub Wait()rnDim PauseTime, Start, Finish, TotalTimePauseTime = 1 ' Set duration.Start = Timer ' Set start time.Do While Timer < Start + PauseTimeDoEvents ' Yield to other processes.LoopFinish = Timer ' Set end time.TotalTime = Finish - Start ' Calculate total time.rnEnd Subrn rnI found all these answers on the web, but I don't remember where I found it to give proper credit.  I wanted to pass along the solution, though.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: Andy Kirby, Chandler Davis, Atos Franzon
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
wahab tunde
Cong Le Duc
Faisal Bashir
Ivica Cvetkovski
Blaine Cox
Shankar Srinivasan
riyepa fdgf
Hannah Cave
Len Matthews
Kristine Arthy
Forum Stats:
Groups: 3
Forums: 24
Topics: 6205
Posts: 27210

 

Member Stats:
Guest Posters: 49
Members: 31878
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.