Macros running concurrently - need 1 to finish before other begins

bluefish44

Board Regular
Joined
Apr 3, 2009
Messages
190
I have two macros which I have strung together into 1 long macro.

The first macro refreshes a bunch of sheets with Power query.
The second macro populates cells with formulas and drags down the formulas next to the updated queries.
When I run 1 macro, wait for it to finish and then run the second, things work fine.
Problem is, when I combine them into 1 long macr,o the 2nd part begins dragging my formulas down before the queries are completed refreshing.
Is the a way to delay the second part of the macro from starting until the entire refresh of the power query is complete?

Thanks in advance
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
For reference - this is the first bit of code I need to completely finish before the second code begins:

Dim ws As Worksheet


For Each ws In ActiveWorkbook.Worksheets


ws.Visible = xlSheetVisible


Next ws




'run power query






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
 
Upvote 0
Instead of combining the two macros, have the first call the second just before the End Sub line of the first macro.
 
Upvote 0
Hi JoeMo - thanks for the response -

I have added Call Macro2 to the bottom of the first macro before the End Sub instead of combining the 2 macros, but the same problem persists.
Macro2 finishes before the query from the first macro is complete.
 
Upvote 0
Hi JoeMo - thanks for the response -

I have added Call Macro2 to the bottom of the first macro before the End Sub instead of combining the 2 macros, but the same problem persists.
Macro2 finishes before the query from the first macro is complete.
I'm not familiar with the particular type of query you are using so this is just a couple of shots in the dark.

First try adding this line after the cn.Refresh bit

DoEvents

If that doesn't work try:

Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 10) 'change 10 (seconds) to suit

The latter should allow recalculation to occur while pausing the macro.
 
Upvote 0
Thanks JoeMo -

What I did was add this line of code at the end of the first macro and it did the trick....

Application.OnTime Now() + TimeValue("00:00:30"), "Macro2"
 
Upvote 0

Forum statistics

Threads
1,214,630
Messages
6,120,634
Members
448,973
Latest member
ChristineC

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