I am using the following QueryTable Inquiry. After the .Refresh
executes the VBA procedure ends. The inquiry works but I need to execute code after it complete
A github repo that demonstrates the minimum code needed to get this working can be found here.
As mentioned in the other answers, the key factors to ensuring you catch the event are:
Declare a global variable of your event-handling class module's type outside of any subroutines/methods, at the top of a file (I chose the ThisWorkbook
file).
Add a Workbook_Open
event handler and instantiate that variable there, so that it is available immediately and will remain in scope (since it's global).
At that point, or at any downstream point when you have a QueryTable you're interested in, pass that QueryTable to the global instance to wire up its events.
(It took me a couple tries to figure this out myself, when someone pointed me in this direction as an answer to this question.)