替换。以雅虎金融的货币金额计算

时间:2015-04-07 09:03:50

标签: excel vba excel-vba excel-2010 yahoo-finance

我正在使用excel 2010。 我有以下vba函数,它从雅虎财务中检索我的货币。但是,我的问题是我使用的是德语版的Excel,.在此版本中是,

因此转换后的货币看起来像这样:

enter image description here

这就是我正在使用的功能:

Function MYCURRENCYEXCHANGER(SourceCur As String, DestCur As String) As Variant
    Dim url As String
    ' http://quote.yahoo.com/d/quotes.csv?s=XXXYYY=X&f=l1  this is the link format where XXX is currency1 and YYY is currency2 '
    url = "http://quote.yahoo.com/d/quotes.csv?s=" & SourceCur & DestCur & "=X&f=l1"
    Dim myHTTP As New WinHttp.WinHttpRequest
    myHTTP.Open "GET", url, False
    myHTTP.send ""
    If myHTTP.StatusText <> "OK" Then GoTo ServerErrorHandler
    If Not (WorksheetFunction.IsNumber(myHTTP.responseText)) Then MYCURRENCYEXCHANGER = 0
    MYCURRENCYEXCHANGER = CDbl(myHTTP.responseText)
    Exit Function

ServerErrorHandler:
    MsgBox "Error. Could not convert currency"

End Function

是否有任何建议如何正确转换货币,以便在从雅虎财务中提取时.,取代?

我赞美你的回复!

1 个答案:

答案 0 :(得分:1)

您可以使用“替换”功能来实现这一目的。

Function MYCURRENCYEXCHANGER(SourceCur As String, DestCur As String) As Variant
  Dim url As String
  ' http://quote.yahoo.com/d/quotes.csv?s=XXXYYY=X&f=l1  this is the link format where XXX is currency1 and YYY is currency2 '
  url = "http://quote.yahoo.com/d/quotes.csv?s=" & SourceCur & DestCur & "=X&f=l1"
  Dim myHTTP As New WinHttp.WinHttpRequest

  myHTTP.Open "GET", url, False
  myHTTP.send ""
  If myHTTP.StatusText <> "OK" Then GoTo ServerErrorHandler

  Replace(myHTTP.StatusText, ".", ",")

  If Not (WorksheetFunction.IsNumber(myHTTP.responseText)) Then MYCURRENCYEXCHANGER = 0
  MYCURRENCYEXCHANGER = CDbl(myHTTP.responseText)
  Exit Function

ServerErrorHandler:
  MsgBox "Error. Could not convert currency"

End Function

Replace(string,searchtext,replacetext)函数将通过字符串中的replacetext更改所有出现的searchtext。

我没有测试过,但我相信这应该可以解决问题。