[VBA] Data from Morningstar website to excel

mikew8

New Member
Joined
Aug 9, 2019
Messages
2
Hi,

Im very new to VBA and I'm trying to run a script which I found on this forum from 2012 posted by "Beeferino". I made 2 changes, the first being the URL as I don't think the original link works anymore and secondly I changed the word from InternetExplorer -> MicrosoftEdge, which I hope will not cause any problems.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
 If Target.Row = Range("Fund_Code").Row And _
 Target.Column = Range("Fund_Code").Column Then
  
  Dim IE As New MicrosoftEdge
   'ET.Visible = True
  IE.navigate "https://www.morningstar.ca/ca/report/fund/performance.aspx?t=0P000072KJ&FundServCode=" & Range("Fund_Code").Value
  Do
   DoEvents
  Loop Until IE.readyState = READYSTATE_COMPLETE
  Dim Doc As HTMLDocument
  Set Doc = IE.document
  
  Dim sAA As String
  sAA = Trim(Doc.getElementsByTagName("span")(20).textContent)
  Range("MER").Value = sAA
  
  Dim sAB As String
  sAB = Trim(Doc.getElementsByTagName("label")(0).textContent)
  Range("Fund_Name").Value = sAB
  
  Dim sAC As String
  sAC = Trim(Doc.getElementsByTagName("span")(30).textContent)
  Range("Investment_Style").Value = sAC
 End If
 
End Sub


I run it under my Sheet (not a Module). It throws an Compile Error: User-defined type not defined. I believe its purpose is when entering fund codes, to output specific values about each mutual fund (MER, Fund name etc.) from the Morningstar website.

If anyone could help me getting this code going, I'd really appreciate it as its a great learning experience in my area of interest.

Kindest Regards,
M
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Pretty sure you can't use any browser other than IE unless you use an add-in like Selenium. But this seems to work.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 
 If Target.Row = Range("Fund_Code").Row And _
 Target.Column = Range("Fund_Code").Column Then
  
  Dim IE As Object: Set IE = CreateObject("InternetExplorer.Application")
   'ET.Visible = True
  IE.navigate "https://www.morningstar.ca/ca/report/fund/performance.aspx?t=0P000072KJ&FundServCode=" & Range("Fund_Code").Value
  Do
   DoEvents
  Loop Until IE.readyState = 4
  Dim Doc As Object
  Set Doc = IE.document
  
  Dim sAA As String
  sAA = Trim(Doc.getElementsByTagName("span")(20).textContent)
  Range("MER").Value = sAA
  
  Dim sAB As String
  sAB = Trim(Doc.getElementsByTagName("label")(0).textContent)
  Range("Fund_Name").Value = sAB
  
  Dim sAC As String
  sAC = Trim(Doc.getElementsByTagName("span")(30).textContent)
  Range("Investment_Style").Value = sAC
 End If
 
End Sub
 
Last edited:
Upvote 0
Thank you for your help, I'm getting a run-time error '1004: Method 'Range' of object'_Worksheet' failed.

I'm unsure how to fix it.


Mike
 
Upvote 0

Forum statistics

Threads
1,214,574
Messages
6,120,329
Members
448,956
Latest member
Adamsxl

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