I am creating some code where I can click on a single button and it will refresh the querytables that I have on that sheet.
Now, my problem is that I have more code after the fresh that copies some of the information, but this code is being run right after the refresh has started and the information has not yet been replaced.
I want to create a waiting period for the refresh to complete and then the rest of the code can continue.
I don't want to just wait for 5 seconds but for the refreshing period, so that I am not waiting too long or too short, depending on Internet speed etc.
How can I do this?
Edit:
Simple code:
ActiveWorkbook.RefreshAll
Here I need the delay or waiting code till all the refreshing is finished... Then
MsgBox("The Refreshing is Completed!")
Something in that direction. But it can't say the msgbox before it is actually finished.... Sometimes depending on internet speed the refreshing takes shorter or longer, so I want it to be a variable of the actual refreshing time.
VBA Wait is a built-in function to pause the code from executing for a specified time. It is very similar to what we do in the Sleep command. To pause a code, we use the Application. Wait method.
Steps to use VBA Wait First, use the keyword “Application” and type a dot (.) to get the list of properties and methods. After that, select or type the “Wait” method. Now, specify the “Time” argument to tell VBA that how much time you want to wait. In the end, run the code to put wait for all the activities in Excel.
Update all data in the workbook Press CTRL+ALT+F5, or on the Data tab, in the Connections group, click Refresh All.
DoEvents is most useful for simple things like allowing a user to cancel a process after it has started, for example a search for a file. For long-running processes, yielding the processor is better accomplished by using a Timer or delegating the task to an ActiveX EXE component.
I was working with a PowerPivot model, and I wanted to Refresh the data before I saved and closed the Model. However, excel just closed the model before the refresh was complete, and the model resumed refreshing on opening.
Adding the following line right after the RefreshAll method, did the trick:
ThisWorkbook.RefreshAll Application.CalculateUntilAsyncQueriesDone
I hope it works for you too.
Make sure that you Disable Events to speed things up.
Note that I am using Excel 2010, I am not sure if this method is available in older versions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With