+ Reply to Thread
Results 1 to 9 of 9

Select sheet after Power Query Update

Hybrid View

  1. #1
    Registered User
    Join Date
    05-28-2012
    Location
    Montana
    MS-Off Ver
    Excel 2010
    Posts
    41

    Select sheet after Power Query Update

    I have a macro that refreshes all my power query connections. After this is refreshed, the process quickly goes through each worksheet to apparently update the tables on each sheet. The problem is that I want to select the "Main" sheet after this refresh is done, but no matter what, the tables are updated as the last step. I've tried to call another macro to select the main sheet. I've also tried putting in the application.wait command. Nothing works. It always updates the tables last. Here's what I've got so far with the code...
    Sub PQ_Refresh()
    
    
    
    Dim lCnt As Long
    
        
        With ActiveWorkbook
            For lCnt = 1 To .Connections.Count
              
              If .Connections(lCnt).Type = xlConnectionTypeOLEDB Then
                .Connections(lCnt).OLEDBConnection.BackgroundQuery = False
              End If
            Next lCnt
        End With
        
        ActiveWorkbook.Connections("Query - CK Elevation").Refresh
    ActiveWorkbook.Connections("Query - AF Discharge").Refresh
    ActiveWorkbook.Connections("Query - AF nflow").Refresh
    ActiveWorkbook.Connections("Query - HP Elevation").Refresh
    ActiveWorkbook.Connections("Query - BD Discharge").Refresh
    ActiveWorkbook.Connections("Query - IN Elevation").Refresh
    ActiveWorkbook.Connections("Query - BC Discharge").Refresh
    
    
    
    
    Sheets("Main").Select
        
        
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    09-30-2018
    Location
    Vlaams Brabant Belgium
    MS-Off Ver
    365
    Posts
    456

    Re: Select sheet after Power Query Update

    Try

    Sheets("Main").Activate

  3. #3
    Registered User
    Join Date
    05-28-2012
    Location
    Montana
    MS-Off Ver
    Excel 2010
    Posts
    41
    Nope, doesn't work. In fact, it doesn't seem to matter which line of code I put at the end of the macro; it always updates the data tables last. Leaving me on the wrong worksheet.
    Last edited by AliGW; 12-02-2019 at 04:11 AM. Reason: Please don't quote unnecessarily!

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: Select sheet after Power Query Update

    Try:

    Application.calculateuntilasyncqueriesdone
    Sheets("Main").Select
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Registered User
    Join Date
    05-28-2012
    Location
    Montana
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Select sheet after Power Query Update

    Quote Originally Posted by rorya View Post
    Try:

    Application.calculateuntilasyncqueriesdone
    Sheets("Main").Select
    That didn't work either. It does seem to finish refreshing the query connections themselves, then the program will select the main sheet, unfortunately after that it updates all the data table associated with the queries, still leaving me on the wrong worksheet.

    I am surprised this is giving me this much trouble!

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: Select sheet after Power Query Update

    So you're saying that Excel actually activates each worksheet with a querytable on it, even though that's not in your code?

  7. #7
    Registered User
    Join Date
    05-28-2012
    Location
    Montana
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Select sheet after Power Query Update

    That is correct. I even tried a different piece of code, same result....

    Public Sub UpdatePowerQueries()
    
    
    Dim lTest As Long, cn As WorkbookConnection
    On Error Resume Next
    For Each cn In ThisWorkbook.Connections
    lTest = InStr(1, cn.OLEDBConnection.Connection, "Provider=Microsoft.Mashup.OleDb.1", vbTextCompare)
    If Err.Number <> 0 Then
    Err.Clear
    Exit For
    End If
    If lTest > 0 Then cn.Refresh
    Next cn
    
    Application.CalculateUntilAsyncQueriesDone
    Sheets("Main").Select
    
    End Sub

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,985

    Re: Select sheet after Power Query Update

    Have you tried a simpler approach like:

    Sub PQ_Refresh()
    Application.screenupdating = false
    ActiveWorkbook.RefreshAll
    Application.CalculateUntilAsyncQueriesDone
    Sheets("Main").Select
    End Sub

  9. #9
    Registered User
    Join Date
    05-28-2012
    Location
    Montana
    MS-Off Ver
    Excel 2010
    Posts
    41

    Re: Select sheet after Power Query Update

    Rorya,

    I tried your code and it had the same result - it cycled through all the worksheets and did NOT return to the Main worksheet.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Update pivot tables without refreshing power query
    By Acceptablerisk in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-25-2019, 04:48 AM
  2. [SOLVED] Power Pivot does not load new column added in Power query
    By ibuhary in forum Excel General
    Replies: 12
    Last Post: 02-19-2019, 03:53 AM
  3. [SOLVED] Select Rows between blank rows using Power Query
    By beaglesBuddy in forum Excel General
    Replies: 5
    Last Post: 08-17-2018, 08:30 PM
  4. Sub-Forum for Excel Power Tools (Power Query, Power Pivot & Power BI)
    By chullan88 in forum Suggestions for Improvement
    Replies: 10
    Last Post: 06-28-2018, 02:25 PM
  5. Replies: 0
    Last Post: 04-05-2018, 01:16 AM
  6. Power Query Won't Update
    By Justair07 in forum Excel General
    Replies: 9
    Last Post: 09-29-2016, 04:46 PM
  7. automate power query update from new tab (office excel 2016)
    By ajnuna in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-18-2016, 07:27 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1