Landzir101
Board Regular
- Joined
- Jun 21, 2009
- Messages
- 78
I have the following code:
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?
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?