0

我的函数从外部网站提取汇率。

我可以提取特定日期的单一费率。

当我有一个不同日期的列表并将函数复制粘贴到整个列表时,我收到错误 91。(我告诉 Excel 为每个特定日期应用此功能。)

这是我的代码(xDoc 对象创建方法的功劳归功于 Analystcave.com/vba-xml-working-xml-files/ 上的 AnalystCave):

Public Function GetCurrToUZS(ByRef Curr As String, ByRef date_param As Date) As Currency        
    Dim xDoc As Object
    Dim xParent As Object
    Dim getRateChild As Object
    Dim corrDate As String

    On Error GoTo errorHandler:

    If Len(Curr) <> 3 Then
        MsgBox "Current identifier should be 3 letters in lenght", vbCritical + vbOKOnly _
            , "ERROR!"
        Exit Function
    End If
    'transforms the entered date to the required format of "YYYY-MM-DD"
    corrDate = Year(date_param) & "-" & Month(date_param) & "-" & Day(date_param)

    Set xDoc = CreateObject("MSXML2.DOMDocument")
    With xDoc
        .async = False
        .validateOnParse = False
        .Load "http://cbu.uz/ru/arkhiv-kursov-valyut/xml/" & Curr & "/" & corrDate & "/"
    End With

    'Get Document Elements
    Set xParent = xDoc.DocumentElement
    Set getRateChild = xParent.ChildNodes(0).ChildNodes(7)

    GetCurrToUZS = getRateChild.Text 'output of the function

    Set xDoc = Nothing 'terminates xDoc Object
    Exit Function

errorHandler:
    MsgBox Err.Number, vbCritical + vbOKOnly, "Critical Error!"
    Exit Function
End Function

作为错误的示例,我在 Dropbox 上创建了这个小的 Excel 文件(https://www.dropbox.com/s/dg2j6o4xjr9v488/FX%20Rate%20Extraction%20Error%20%28stackoverflow%29.xlsx?dl=0 ) 列出日期。第一个是使用这个函数完成的,应该很容易提取速率,没有任何错误。将公式复制粘贴到所有其他日期后,将出现错误 91。

4

1 回答 1

0

错误 91 表示未设置对象。

您最有可能的赌注是xDoc不能总是从您指定的 URL 中检索到。如果我转到http://cbu.uz/ru/arkhiv-kursov-valyut/xml/usd/14.01.17(表格中的第 3 个日期),您将获得11.07.2017. 事实上,如果你去http://cbu.uz/ru/arkhiv-kursov-valyut/xml你会看到所有提供的记录都是针对那个特定日期的。

围绕无法获取xDoc和随后无法设置进行xParent错误处理getRateChild,它应该像一个魅力一样工作。

于 2017-07-17T07:07:37.427 回答