As commented, the specific URL posted is an XML that uses an XSLT stylesheet to render page as HTML. But underlying source and therefore the response text is XML. See XML data source with Ctrl+U:
XML
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="http://www.floatrates.com/currency-rates.xsl" ?>
<channel>
<title>XML Historical Foreign Exchange Rates for U.S. Dollar (USD) (4 Feb 2021)</title>
<link>http://www.floatrates.com/currency/usd/</link>
<xmlLink>http://www.floatrates.com/daily/usd.xml</xmlLink>
<description>XML historical foreign exchange rates for U.S. Dollar (USD) from the Float Rates. Published at 4 Feb 2021.</description>
<language>en</language>
<baseCurrency>USD</baseCurrency>
<pubDate>Thu, 4 Feb 2021</pubDate>
<lastBuildDate>Thu, 4 Feb 2021</lastBuildDate>
<item>
<title>1 USD = 0.832481 EUR</title>
<link>http://www.floatrates.com/usd/eur/</link>
<description>1 U.S. Dollar = 0.832481 Euro</description>
<pubDate></pubDate>
<baseCurrency>USD</baseCurrency>
<baseName>U.S. Dollar</baseName>
<targetCurrency>EUR</targetCurrency>
<targetName>Euro</targetName>
<exchangeRate>0.832481</exchangeRate>
<inverseRate>1.201229</inverseRate>
<inverseDescription>1 Euro = 1.201229 U.S. Dollar</inverseDescription>
</item>
<item>
<title>1 USD = 0.733621 GBP</title>
<link>http://www.floatrates.com/usd/gbp/</link>
<description>1 U.S. Dollar = 0.733621 U.K. Pound Sterling</description>
<pubDate></pubDate>
<baseCurrency>USD</baseCurrency>
<baseName>U.S. Dollar</baseName>
<targetCurrency>GBP</targetCurrency>
<targetName>U.K. Pound Sterling</targetName>
<exchangeRate>0.733621</exchangeRate>
<inverseRate>1.363101</inverseRate>
<inverseDescription>1 U.K. Pound Sterling = 1.363101 U.S. Dollar</inverseDescription>
</item>
...
</channel>
But you can still parse the response return and run XPath on <item>
node data. Simply use MSXML's DomDocument
with LoadXML
and SelectNodes
methods.
VBA
Sub CallFunc()
Call GetHistoricFX("USD", "", "2021-02-04")
End Sub
Function GetHistoricFX(fromCurr As String, toCurr As String, AsofDate As Date) As String
On Error GoTo ErrHandle
Dim xmlHttp As Object
Dim sUrl As String
Dim xmldoc As Object, itemNodes As Object, itemNode As Variant, chNode As Variant
Dim i As Long, j As Long
' Create an XMLHTTP object
Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
' get the URL to open
sUrl = "http://www.floatrates.com/historical-exchange-rates.html?" _
& "currency_date=" & AsofDate _
& "&base_currency_code=" & fromCurr _
& "&format_type=xml"
' open connection and get data
xmlHttp.Open "GET", sUrl, False
xmlHttp.send
' CREATE A DOMDocument OBJECT FROM RESPONSE
Set xmldoc = CreateObject("MSXML2.DOMDocument")
xmldoc.LoadXML xmlHttp.responseText
xmldoc.setProperty "SelectionLanguage", "XPath"
Set itemNodes = xmldoc.SelectNodes("//item")
' ITERATE THROUGH ITEM NODES AND CHILDREN
With ThisWorkbook.Worksheets("MAIN")
i = 2
For Each itemNode In itemNodes
j = 1
For Each chNode In itemNode.SelectNodes("*")
If i = 2 Then
.Cells(i - 1, j) = chNode.tagName
End If
.Cells(i, j).Value = chNode.Text
j = j + 1
Next chNode
i = i + 1
Next itemNode
End With
MsgBox "Successfully completed!", vbInformation
ExitHandle:
Set chNode = Nothing
Set itemNode = Nothing
Set itemNodes = Nothing
Set xmldoc = Nothing
Set xmlHttp = Nothing
Exit Function
ErrHandle:
MsgBox Err.Number & " - " & Err.Description, vbCritical
Resume ExitHandle
End Function
Output
CreateObject("xmlfile")
- do you have a reference for that? – Tim Williamsformat_type=xml
you load a XML. So you can't work with.getElementsByClassName("row")
. But you can switch to HTML withformat_type=html
. I have checked that withDebug.Print xmlHttp.responseText
. But there are other errors in your code.RateFound
is not needed. But if you want it it's on the wrong position and it is not declared. Also you don't want theinnertext
ofTDelement
because that'stoCurr
. I have no time now to look further into the code. – Zwenn