Do While Loop Is Stuck

ajax1231

New Member
Joined
Sep 12, 2021
Messages
4
Office Version
  1. 2019
Platform
  1. Windows
Hi,
I have been working on putting together a web query with a loop to wait for the URL to load completely. I have gotten this to work, but I am unable to get the loop to end once this happens and to then complete the query. Any suggestions?

Sub Data()
Dim r As Long
Dim QT As QueryTable
Dim URL As String
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True


With Worksheets("Company Data")
For r = 2 To .Range("C" & .Rows.Count).End(xlUp).Row
URL = http://financials.morningstar.com/ratios/r.html?t=" & .Range("C" & r).Value & "&region=usa&culture=en-US"
IE.navigate URL
Do While IE.Busy Or IE.ReadyState <> READYSTATE_COMPLETE
DoEvents
Loop
With Sheets("Company").QueryTables.Add(Connection:="URL;" & URL, Destination:=Sheets("Company").Range( _
"$A$1"))
.Name = "company_1"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = True
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Next r
End With
End Sub
 
Last edited by a moderator:

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Maybe something like this?

VBA Code:
    Dim TimeOut As Double
    TimeOut = Timer
    Do While IE.busy Or IE.READYSTATE <> 4
        DoEvents
        If Timer - TimeOut > 60 Then '60 second timeout
            MsgBox "Early Exit. Problem getting data from website"
            Exit Do
        End If
    Loop
 
Upvote 0
Solution
Thank you, that worked for ending my loop once the URL was fully loaded and completing the query.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,927
Members
449,094
Latest member
teemeren

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