Run VBA after ALL tables have refreshed

MichaelSchulz

Board Regular
Joined
Apr 10, 2014
Messages
64
I want to have a block of VBA code run after all the tables in the workbook have refreshed:
  1. User clicks the Refresh All button which causes
  2. All tables in the workbook to be refreshed
    1. four tables, each on a separate sheet
    2. tables are refreshed via Power Query
  3. Once all tables have completed the refresh, the VBA code runs
I have accomplished this in a different project using WithEvents and setting a QueryTable variable but that was only one table being refreshed. I just can't seem to see the logic to extend that approach to encompass multiple tables.

The code used for one table, set in ThisWorkbook:
VBA Code:
Option Explicit
Private WithEvents QT as QueryTable

Private Sub Workbook_Open()
       Set QT = Sheet1.ListObjects(1).QueryTable
End Sub

Private Sub QT_AfterRefresh(ByVal Success As Boolean)
        If Success Then
              [I][block of code to be run after refresh completes][/I]
        End If
End Sub
 
What I'm saying is the code I posted does work with the Refresh All button but for only one table.

In that prior project, the user clicks the Refresh All button on the ribbon, the code detects when that one table has finished the refresh and then runs the rest of the code.

But the code---as it is---seems limited to one table. At the very least, I can't seem to think of the way to extend it to all tables.
 
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I think I may have a solution to this but I want to ask this again with a clearer explanation before I know whether this is a good answer or not.
 
Upvote 0
I have an Excel workbook containing multiple Query Tables.

To refresh the data in all the Query Tables, I want the User to click the Refresh All button on the Ribbon.

I have a block of VBA code I want to run automatically once all the tables have completed being refreshed.

Over time, the number of Query Tables within the workbook may change, e.g. a new table may get added and/or a table may get removed.



How can this be accomplished?
 
Upvote 0
I think I may have a solution to this but I want to ask this again with a clearer explanation before I know whether this is a good answer or not.
When you come up with a solution and post it, then it is perfectly fine to mark your own post as the solution to help future readers. Otherwise, please do not mark a post that doesn't contain a solution.
 
Upvote 0
@MichaelSchulz

In future, please do not post the same question multiple times. Per Forum Rules (#12), posts of a duplicate nature will be locked or deleted.

In this case I have moved your 'clearer explanation' new thread post to your existing thread on this topic.
 
Upvote 0
While it's possible to assign your own macro to a built-in control, such as RefreshAll, I would suggest that you create a custom button within a custom group in the built-in Table Design tab. Then, once the button is clicked, it will refresh all and run your macro. See the attached image.

refresh_all.png


Also, if you're going to need this for a number of users, you can create an add-in and distribute it to them.

Would this work for you?
 
Upvote 0
When you come up with a solution and post it, then it is perfectly fine to mark your own post as the solution to help future readers. Otherwise, please do not mark a post that doesn't contain a solution.
While it's possible to assign your own macro to a built-in control, such as RefreshAll, I would suggest that you create a custom button within a custom group in the built-in Table Design tab. Then, once the button is clicked, it will refresh all and run your macro. See the attached image.

View attachment 65771

Also, if you're going to need this for a number of users, you can create an add-in and distribute it to them.

Would this work for you?
I am curious to know how to do this with the user clicking the Refresh All button.
 
Upvote 0
Basically, you would need to use RibbonX XML code to assign your own macro to the RefreshAll control. So when you click on RefreshAll, your code would run instead. Your code would be written so that it first refreshes all tables, and then continues with your other code.

Just to give you an idea of whats involved, first you would need to download the Custom UI Editor to help with adding the XML code to your workbook...


Then you would add the neccessary XML code and VBA code to your workbook, as per the following instructions...


At first glance, it might look like it's a little too complicated. But it's actually not as bad as it looks. If you would like to give it a try, let me know and I can provide you with the necessary XML code and VBA code.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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