neodjandre
Well-known Member
- Joined
- Nov 29, 2006
- Messages
- 950
- Office Version
- 2019
- Platform
- Windows
I have been looking around the internet with no success for a VBA function which will simply take 3 arguments: Currency From, Currency To, Date and output the relevant FX Rate. Has anyone worked with a successful solution?
I am currently using this procedure which works fast but it outputs a table in a sheet. A function would be much more useful. e.g. fx_convert("USD","GBP",30/01/2015)
many thanks,
Andrew
I am currently using this procedure which works fast but it outputs a table in a sheet. A function would be much more useful. e.g. fx_convert("USD","GBP",30/01/2015)
Code:
Public Sub GetTable()
Dim sResponse As String, html As HTMLDocument, ws As Worksheet, clipboard As Object
Set ws = ThisWorkbook.Worksheets("Sheet1")
With CreateObject("MSXML2.XMLHTTP")
.Open "GET", "https://www.x-rates.com/historical/?from=CAD&amount=1&date=" & Format$(Date - 1, "yyyy-mm-dd"), False
.setRequestHeader "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"
.send
sResponse = StrConv(.responseBody, vbUnicode)
End With
Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
Set html = New HTMLDocument
With html
.body.innerHTML = sResponse
clipboard.SetText .querySelectorAll(".ratesTable").item(1).outerHTML
clipboard.PutInClipboard
End With
ws.Cells(1, 1).PasteSpecial
End Sub
many thanks,
Andrew