Running Power Query in a Closed Workbooks

hananak

Board Regular
Joined
Feb 10, 2022
Messages
110
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi All,

I have two workbooks. They both are directly linked with Oracle Accounting software through Power Query. Every time when we want to see latest data. I have to go to those workbooks one by one. Run the query (once the query has finished running), save the files and close. After that I have separate file which contains all the Pivot tables reports that have connections to those workbooks. All I do is "Refresh All" and all the pivot tables reports are refreshed with the new data in them.

What I am trying to do is to have a button in my Pivot tables reports workbook. When I press it. It should run the queries in those closed workbooks which are directly linked with Oracle Accounting software. and alert me once the queries have successfully fetched the new data and then refresh all the pivot reports.

Currently, I have to go several times in those workbooks to run the queries, save and close during the day. It has becomes really frustrating to open the files, run the queries, save and close and then again do the same process.
There are not set timings otherwise I would have chosen to run the queries automatically after specific time interval.

I really appreciate in advance for your help please!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Is there a reason you can't have the queries in the same workbook as the pivot tables?
 
Upvote 0
Can anyone confirm whether it is possible please?
Thanks for your reply.

Yes, those two workbook contains huge data and very heavy in size. Previously we used have them in pivot table reports file but it made it really really slow. The files used to crash a lot, while we are analysing the data.
 
Upvote 0
Then you don't have a lot of options, I'm afraid. You can't refresh a PQ query without having the file open.
 
Upvote 0
Then you don't have a lot of options, I'm afraid. You can't refresh a PQ query without having the file open.
Is it possible to automatically open the file in the background, hide it and once the PQ has finished running then close and save the file?

Is it possible by utilizing VBA, can we do that?
 
Upvote 0
If you have the queries set up in the data workbook to not refresh in the background, then you can use something like:

Code:
With GetObject("path to workbook here")
   .Refreshall
   .Close Savechanges:=true
end with

for each source workbook.
 
Upvote 0
If you have the queries set up in the data workbook to not refresh in the background, then you can use something like:

Code:
With GetObject("path to workbook here")
   .Refreshall
   .Close Savechanges:=true
end with

for each source workbook.
Sorry, I am not fully familiar with VBA.

Should I post this code in VBA module in Pivot tables report file?

I tried to do that but it gives an error.
 

Attachments

  • Error.PNG
    Error.PNG
    69.9 KB · Views: 36
Upvote 0
As a full macro, it would be something like (do not include code tags when pasting to the VB editor):

VBA Code:
Sub UpdateSourceDataFiles()
With GetObject("path to workbook here")
   .Refreshall
   .Close Savechanges:=true
end with
End Sub
 
Upvote 0
I ran the code and it didn't run the PQ correctly and instead made the original file totally blank and all the data is lost. See the attached picture for your reference.
 

Attachments

  • Error 2.PNG
    Error 2.PNG
    44.7 KB · Views: 31
Upvote 0

Forum statistics

Threads
1,223,943
Messages
6,175,546
Members
452,652
Latest member
eduedu

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