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?

[TABLE="width: 0"]
<tbody>[TR]
[TD]AUDUSD
[/TD]
[/TR]
[TR]
[TD]USDCAD
[/TD]
[/TR]
[TR]
[TD]USDCHF
[/TD]
[/TR]
[TR]
[TD]EURUSD
[/TD]
[/TR]
[TR]
[TD]GBPUSD
[/TD]
[/TR]
[TR]
[TD]USDJPY
[/TD]
[/TR]
[TR]
[TD]NZDUSD
[/TD]
[/TR]
[TR]
[TD]USDCNH
[/TD]
[/TR]
</tbody>[/TABLE]

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.

[TABLE="width: 0"]
<tbody>[TR]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

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,223,952
Messages
6,175,595
Members
452,657
Latest member
giadungthienduyen

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