Scrape CCY previous closing from marketwatch using VBA

hkese

New Member
Joined
Jun 17, 2014
Messages
15
Anyone has any idea how can I use vba to scrape the previous closing rate of a list of currencies to excel?

AUDUSD
USDCAD
USDCHF
EURUSD
GBPUSD
USDJPY
NZDUSD
USDCNH

<tbody>
</tbody>

For example, if I want to check the previous closing of AUDUSD, I would need to go to the following website, and copy the previous close figure to excel.
But I would need to do the above step for every single currencies. Any way to use vba to scrape such figure?

https://www.marketwatch.com/investing/currency/audusd/historical
https://www.marketwatch.com/investing/currency/usdcad/historical

etc..

Thank you.


<tbody>
</tbody>
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Excel 2010
AB
1USDCAD1.2491
2audusd0.7906
3eurbrl4.0038
4mxnpln0.1805
5usdeur0.8080
Sheet5
Cell Formulas
RangeFormula
B1=Retrieve_Previous_CloseFX(A1)
B2=Retrieve_Previous_CloseFX(A2)
B3=Retrieve_Previous_CloseFX(A3)
B4=Retrieve_Previous_CloseFX(A4)
B5=Retrieve_Previous_CloseFX(A5)


vba function:
Code:
Dim xmlHttpObj As Object
Dim oHTMLDoc As MSHTML.HTMLDocument

Function Retrieve_Previous_CloseFX(ccy As String) As Variant
Dim strURL As String
If xmlHttpObj Is Nothing Then Set xmlHttpObj = CreateObject("MSXML2.XMLHTTP")
If oHTMLDoc Is Nothing Then Set oHTMLDoc = CreateObject("htmlfile")

strURL = "https://www.marketwatch.com/investing/currency/" & ccy & "/historical"
xmlHttpObj.Open "GET", strURL, False
xmlHttpObj.send

oHTMLDoc.body.innerHTML = xmlHttpObj.responseText

Retrieve_Previous_CloseFX = oHTMLDoc.getElementsByClassName("lastcolumn data mrkthours price").Item(0).innerText
End Function

you need to add a referenceto: MICROSOFT HTML OBJECT LIBRARY
 
Upvote 0

Forum statistics

Threads
1,214,845
Messages
6,121,902
Members
449,053
Latest member
Guy Boot

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