Macro too fast for Web Query

Landzir101

Board Regular
Joined
Jun 21, 2009
Messages
78
I have the following code:

Code:
Sub MorningstarPriceTarget()
Dim sPath As String, sName As String
Dim bk As Workbook
Dim sSymbol As String
Dim sDestination As String
'Dim ws As Worksheet
Dim ws As Worksheet
Dim c As Range

Application.DisplayAlerts = False
sPath = "C:\Users\Capital Fortress\Desktop\Capital Fortress Ratings\Archived Reports\Excel Original\"
sName = Dir(sPath & "*.xlsm")
Do While sName <> ""
Set bk = Workbooks.Open(sPath & sName)
With Sheets("Morningstar FV").Select
sSymbol = Range("B6").Value

With ActiveSheet.Range("B16:E54").ClearContents
End With
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://analysis.morningstar.com/analystreport/ar.aspx?symbol=" & sSymbol, _
        Destination:=Range("B16"))
        .Name = "ar.aspx?symbol=" & sSymbol
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = True
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebTables = "1,2,3"
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=True
    End With
End With

        Sheets("Ratings_New").Select
        bk.Close Savechanges:=True
        sName = Dir()
    Loop
End Sub

Which works great except for the fact that it saves and closes the workbook before the web query can run and pull data into the 'Morningstar FV' tab. I tried adding an application wait line in different spots but it still did no good. Is there anyway I can tell the macro to only go on to save and close the workbook after data has been pulled in?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,214,429
Messages
6,119,424
Members
448,896
Latest member
MadMarty

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