Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel vba refresh wait

Tags:

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.

like image 820
dave123 Avatar asked Jan 19 '12 11:01

dave123


People also ask

Is there a wait command in VBA?

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.

How do you use wait in Excel VBA?

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.

How do you refresh data connection in Excel VBA?

Update all data in the workbook Press CTRL+ALT+F5, or on the Data tab, in the Connections group, click Refresh All.

What does DoEvents do in VBA?

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.


1 Answers

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.

like image 156
Ejaz Ahmed Avatar answered Sep 17 '22 16:09

Ejaz Ahmed