Let PowerQueries don't have time to finish with VBA

stigcorneer

New Member
Joined
Nov 14, 2018
Messages
30
Hi

I have a VBA script that is supposed to remove the protection of the sheets, update two PowerQueries and then re-protect the sheets.
When I run my macro, the queries do not finish, which causes me to get errors in the result.
I would like script to pause until the first query is finished, then the next query will run, a new break and then the rest of the macro will continue.
Query2 is based on the result from Query1. So I need the first to finish before I run the next one.
How do I accomplish this?



Code below:


Sheets ("SheetA"). Unprotect
Sheets ("SheetB"). Unprotect


Worksheets ("SheetA"). ListObjects ("Query1"). QueryTable.Refresh BackgroundQuery: = False
Worksheets ("SheetB"). ListObjects ("Query2"). QueryTable.Refresh BackgroundQuery: = False



Sheets ("SheetA"). Protect DrawingObjects: = True, Contents: = True, Scenarios: = True
Sheets ("SheetB"). Protect DrawingObjects: = True, Contents: = True, Scenarios: = True
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
There may be a way to do it with some Query property - but I don't use them often, so I don't know.
But a general pause can be done using Application.Wait.

So, after your first query, you can do
Application.Wait (Now + TimeValue("0:00:10"))
Which tells the code to wait 10 seconds before going on to the next line.
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,310
Members
452,634
Latest member
cpostell

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