首页 > 解决方案 > 从互联网上获取正确的汇率

问题描述

对于我的 Excel 计算表,我需要实际汇率。从互联网上查找和复制汇率没有问题。我的问题是,我从 Internet Explorer 复制的值不会显示为正确的数字格式。

例如,我想要阿根廷比索的汇率(1 欧元 = 49,9341 阿根廷比索)。我从 Internet Explorer 复制该值并希望将其粘贴到我的工作表中,不幸的是,该值的数字格式将显示为 €1 = ARS 499.341

这是我的代码:

'Create Internet Object
Set IE = CreateObject("InternetExplorer.Application")

'IE.Visible = True
IE.Navigate "https://www.finanzen.net/waehrungsrechner/euro_" & currency_value

Do
    DoEvents
Loop Until IE.readystate = 4

value_to_copy = IE.Document.GetElementByID("currency-second-input").Value
currency_value = Split(value_to_copy, " ")
    
Range("L12").Value = "Price in " & currency_value(0)
Range("K8").Value = currency_value(1)  'This value will shown as 499.341

这里是我复制网站汇率的 HTML 部分:

<input value="USD 1,1189" id="currency-second-input" onclick="this.select();" onkeyup="calculateCalculator(true, 'currency-second-input', false)" style="width: 50%; float: left; height: 51px; padding: 10px; font-size: 20px; color: #0066cc; font-weight: bold;" type="text">

在我的测试运行中,这个问题并不总是发生。如果我需要哥伦比亚比索的汇率(1 欧元 = COP 3.623,5878),它将超过正确的值。

标签: excelvba

解决方案


如果你不介意它是一个字符串,你可以在前面粘贴“'”。您可以使用更快的 xhr 而不是打开浏览器。

Option Explicit
'VBE > Tools > References: Microsoft HTML Object Library
Public Sub ExchangeRate()
    Dim html As Object
    Set html = New HTMLDocument
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", "https://www.finanzen.net/waehrungsrechner/euro_argentinischer-peso", False
        .send
        html.body.innerHTML = .responseText
    End With
    ActiveSheet.Cells(1, 1) = "'" & html.getElementById("currencyExchangeRate").Value
End Sub

推荐阅读