OBJECTIVE
Pull in exchange rate data for various currencies.
APPROACH
- Select Active Sheet and copy currencies-to-be-converted into an array (e.g ["EUR", "GBP", "USD"]
- Open Browser and visit currency conversion website
- Loop through different currencies and extract currency conversion factors
- Append conversion factors to an array
- Repopulate excel with newest conversion factors
HTML
<span class="amount" id="converterToAmount" style="">26.21</span>
CODE
Sub retreiveCurrencies()
Dim ws As Worksheet
Dim locals() As Variant
Dim rates As Object
Dim exchangeArray() As Variant
Dim i As Long
Dim IE As Object
'Select currencies to convert
Sheets("APPENDIX - CURRENCY CONVERTER").Activate
locals = ActiveSheet.Range("B2:B15").Value
'This should return locals = ["EUR", "GBP, "USD"]
'Prep Internet Explorer
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
'Loop through currencies and retreive rates. Paste rates into exchangeArray
For i = LBound(locals, 1) To UBound(locals, 1)
IE.Navigate "http://www.usforex.com/currency-converter/" & locals(i, 1) & "/usd/1.00/false"
Do While IE.Busy And Not IE.readyState = READYSTATE_COMPLETE
DoEvents
Loop
'!!!!error on following line = "Object required"!!!!
Set rates = IE.Document.GetElementById("converterToAmount").innerText
ReDim Preserve exchangeArray(rates)
Next i
'Paste exchange rate array into currency conversion column
ActiveSheet.Range("E2:E15") = exchangeArray()
End Sub
QUESTIONS/ISSUE(S)
- Currently getting error "Object Required" @
Set rates = IE.Document.GetElementById("converterToAmount").innerText
despite definingDim rates As Object
. Any solution? - Is
ActiveSheet.Range("E2:E15") = exchangeArray()
sufficient to paste cells back into excel?
rates
shouldn't be an object. You're setting it to the value of the IE element's innertext, which is a string. Try defining it as String and drop theSet
keyword. – Dave.Range("E2").Resize(UBound(exchangeArray, 1), UBound(exchangeArray, 2)) = exchangeArray
– user4039065exchangeArray
back into the worksheet, it should be a two-dimensioned array, even if one of those dimensions is only1 to 1
. – user4039065