ai1094

Board Regular
Joined
Aug 23, 2018
Messages
92
Hi, I have the following VBA code:

Set Wbk1 = ActiveWorkbook
Fname = "Path name"
Set Wbk2 = Workbooks.Open(Fname)
Wbk2.RefreshAll
DoEvents
Wbk2.Sheets(1).Copy , Wbk1.Sheets(Wbk1.Sheets.Count)
Wbk2.Close False

So basically this code is part of a macro that opens ANOTHER workbook which contains a macro and has power query that refreshes the tables to update the latest information. Then I copy the sheet from the second workbook into the first workbook. The issue is after the 'Wkbk2.RefreshAll' executes, it doesn't refresh with the latest information. However, when I run this part of the macro line by line it refreshes. What could be the problem?

Thanks
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,056
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
Hello there. The difficulty is that vba refreshes don't always wait for the next line to be executed. Try this code wherever you want to be sure all database queries are complete:
VBA Code:
Application.CalculateUntilAsyncQueriesDone
So in your case you would put this on the line after the refreshall
 

ai1094

Board Regular
Joined
Aug 23, 2018
Messages
92
I'll give this a try, but where would I insert this line in the code? Also, do I need to remove any other lines of code?

Hello there. The difficulty is that vba refreshes don't always wait for the next line to be executed. Try this code wherever you want to be sure all database queries are complete:
VBA Code:
Application.CalculateUntilAsyncQueriesDone
 

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,056
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
VBA Code:
Set Wbk1 = ActiveWorkbook
  Fname = "Path name"
  Set Wbk2 = Workbooks.Open(Fname)
  Wbk2.RefreshAll
 Application.CalculateUntilAsyncQueriesDone DoEvents
  Wbk2.Sheets(1).Copy , Wbk1.Sheets(Wbk1.Sheets.Count)
  Wbk2.Close False
 

ai1094

Board Regular
Joined
Aug 23, 2018
Messages
92
It looks like this worked. Thanks a lot!

VBA Code:
Set Wbk1 = ActiveWorkbook
  Fname = "Path name"
  Set Wbk2 = Workbooks.Open(Fname)
  Wbk2.RefreshAll
Application.CalculateUntilAsyncQueriesDone DoEvents
  Wbk2.Sheets(1).Copy , Wbk1.Sheets(Wbk1.Sheets.Count)
  Wbk2.Close False
 

Watch MrExcel Video

Forum statistics

Threads
1,133,648
Messages
5,660,105
Members
418,549
Latest member
malmattos

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
Top