excel - 从互联网上获取正确的汇率
问题描述
对于我的 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),它将超过正确的值。
解决方案
如果你不介意它是一个字符串,你可以在前面粘贴“'”。您可以使用更快的 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
推荐阅读
- vuejs3 - VUE3 :: 如何不在历史中保存路线
- c - 如果在驱动程序中看不到预防性抢占,则认为是关键代码
- python - Visual Studio 代码 python (v 3.8) microsoft.interop.excel
- r - 使用手动比例和 Tex 命令复制标签
- javascript - 如何调节班级
- python - 我想使用小数生成一个 for 循环。我需要循环中的 y 值来创建一个列表。这是一个项目,我不能使用 numpy
- css - 是否有支持的 -webkit-tap-highlight-color: transparent 版本?
- antlr - ANTLR - 不匹配的输入错误(在令牌和规则之间)
- c# - CRLF 的 DataContractSerializer 和 WCF 服务响应处理明显不一致
- maven - 如何在一个项目中加入 Maven 多模块 jar?