【问题标题】:Get exchange rates from a certain date with Excel VBA使用 Excel VBA 获取某个日期的汇率
【发布时间】:2021-06-30 20:04:12
【问题描述】:

我想创建一个 Excel 函数,可以获取给定日期两种给定货币之间的汇率。比如:=GetFX("USD","GBP","31/12/2020")

我在Reddit thread 上找到了下面的代码,它正是这样做的,只是它已经过时并且不再与 Oanda 一起使用。我不知道如何将 Excel VBA 与外部数据一起使用,也不知道出了什么问题。如果有人知道如何解决它,我将不胜感激。

谢谢!

Function GetOandaFX(fromCurr As String, toCurr As String, AsofDate As Date) As String

Dim oHttp As Object
Dim sURL As String
Dim HTMLdoc As Object
Dim TDelements As Object
Dim TDelement As Object

' Create an XMLHTTP object
Set oHttp = CreateObject("MSXML2.XMLHTTP")
         
' get the URL to open
 sURL = "https://www.oanda.com/fx-for-business/historical-rates?" _
    & "date=" & AsofDate _
    & "date_fmt=us" _
    & "&exch=" & fromCurr _
    & "&sel_list=" & toCurr _
    & "&value=1&format=HTML&redirected=1"
  
' open connection and get website html
oHttp.Open "GET", sURL, False
oHttp.send
 
Set HTMLdoc = CreateObject("htmlfile")

With HTMLdoc
    If oHttp.readyState = 4 And oHttp.Status = 200 Then 'readystate checks loading, status checks the validity of URL

'    ' assign the returned text to a HTML document
    .body.innerHTML = oHttp.responseText
      
    Set TDelements = .getElementsByTagName("TD")
    'Loop within Table elements
    For Each TDelement In TDelements
        If RateFound = True Then
            GetOandaFX = TDelement.innerText
            Exit For
        End If
        If TDelement.innerText = toCurr Then RateFound = True
    Next
    End If
End With

Set oHttp = Nothing

End Function

【问题讨论】:

标签: excel vba currency


【解决方案1】:

您可以使用VBA.CurrencyExchange中的函数。

该项目所涉及的服务是:

  The European Central Bank
  The Danish National Bank
  The Central Bank of the Russian Federation
  Currency Converter API
  Currencylayer API
  ExchangeRate API
  Fixer
  Open Exchange Rates
  php.mk (National Bank of the Republic of North Macedonia)
  XE

这里的代码太多了,所以研究一下代码和full documentation的链接。

【讨论】:

  • 感谢 Gustav,看起来是个不错的工具,但它似乎是为具有高级编码技能的人量身定制的。他们没有为像我这样的简单用户提供示例 Excel 电子表格(有一个演示文件,但似乎是用于 Access)。不幸的是,我没有完成这项工作的技能。
  • 压缩包中包含一个 Excel 演示供下载。
  • 非常感谢 Gustav,Excel 演示非常有帮助。我现在可以使用 =CurrencyConvertCbr("USD","EUR") 之类的公式来获取当前汇率。但是,我不知道如何添加日期参数来检索过去特定时间点的汇率。这可能吗?
  • 一些服务通过在 URL 中添加一个参数来提供该选项,但我没有检查过(访问每个 ExchangesRate 函数顶部列出的 URL)。这些功能主要用于检索每日汇率,并在您以后需要时存储在本地。一个例子是ExchangeRate-API
  • 感谢 Gustav,我试图弄清楚,但无法成功。我一直在寻找一个简单的解决方案,这对于我的编码技能来说太先进了。我最终使用了一个带有历史数据和 INDEX+MATCH 函数的旧 Excel 表格。它显然不像我想要的那样干净/自动,但它完成了工作。尽管如此,还是非常感谢您的工作,感谢您的分享,我相信它会对具备合适技能的人有所帮助!
猜你喜欢
  • 1970-01-01
  • 2016-11-07
  • 2017-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-04
  • 1970-01-01
相关资源
最近更新 更多