1 second delay

tlindeman

Active Member
Joined
Jun 29, 2005
Messages
313
Could you please supply me witht the vba code that creates a one second delay between running the next section of my code? I have created code that connects to google.com/finance and downloads the financials based on the tickers in A. However, I keep getting errors that the web query is not valid. However, when I refresh the connection it works. I belive that it is moving to fast. Here is a snippet of my code:

If Range("A232").Value = " " Then Exit Sub
With ActiveSheet.QueryTables.Add(Connection:= _
"URL;http://www.google.com/finance?q=" & Range("A232").Value & "&fstype=ii", Destination:=Range("B232"))
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlAllTables
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery = False
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
This is borrowed from an old project that I did. I hope it works and that i've not missed a bit of the code out somewhere lol

You need to create a module and insert the following at the top:

Code:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Then, in the middle of your code:

Code:
Sleep 1000

This would cause it to pause for 1 second (1000 miliseconds)
 
Upvote 0
Add this line of code where you want the delay:
Code:
Application.Wait (Now + TimeSerial(0, 0, 1))
If you want more delay, change the "1" to the number of seconds you want the delay.
 
Upvote 0
I had an alternate version that i've found in case you had any problems with the first one.

Code:
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 1
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime

Edit - lol beaten to it, but with a much more efficient version :p
 
Upvote 0
You might also change

.BackgroundQuery = True
to
.BackgroundQuery = False


This should force VBA to wait for the query to complete before resuming it's next command.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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