VBA Error?

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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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