mattyj7183
New Member
- Joined
- Dec 28, 2015
- Messages
- 15
- Office Version
- 365
- Platform
- Windows
For starters, I am relatively new to VBA and adjusted the below code from a former co-workers code that pulled different info. I have looked around for a day or so and finally need the help from the masters.
In col A I have a list of mutual funds and stocks that I need to pull the category information from a public website. I have two different macros (one for stocks and one for mutual funds) to get the category tags - the URLs are different hence the 2 Macros. The below macro is for Mutual Funds, but they are essentially the same. For testing, I have truncated the list to go from A2:A5, and it takes in upwards of 2 minutes to pull those 3 data points. The final list will be in the 1,500ish and will take a ton of time. I know part of the time is the (Now + TimeValue("0:00:05"). I initially had (Now + TimeValue("0:00:02") but the macro was skipping tickers in col A. There must be a better way to optimize this code?
TIA!
In col A I have a list of mutual funds and stocks that I need to pull the category information from a public website. I have two different macros (one for stocks and one for mutual funds) to get the category tags - the URLs are different hence the 2 Macros. The below macro is for Mutual Funds, but they are essentially the same. For testing, I have truncated the list to go from A2:A5, and it takes in upwards of 2 minutes to pull those 3 data points. The final list will be in the 1,500ish and will take a ton of time. I know part of the time is the (Now + TimeValue("0:00:05"). I initially had (Now + TimeValue("0:00:02") but the macro was skipping tickers in col A. There must be a better way to optimize this code?
TIA!
Code:
Sub Mutual_Funds()
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Dim IE As Object
Set Rng = Range("A2:A5")
Set Row = Range(Rng.Offset(1, 0), Rng.Offset(1, 0).End(xlDown))
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = False
For Each Row In Rng
.navigate "http://quotes.morningstar.com/chart/fund/chart?t=" & Range("A" & Row.Row).Value & "®ion=usa&culture=en_US"
Application.Wait (Now + TimeValue("0:00:05"))
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim doc As HTMLDocument
Set doc = IE.document
While IE.readyState <> 4
Wend
On Error Resume Next
Set Rng = Range("c2:c5")
Range("C" & Row.Row).Value = doc.getElementsByClassName("gr_text1")(10).innerText
Next Row
IE.Quit
Set IE = Nothing
End With
MsgBox (" Macro Completed ")
'
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
End Sub