从外部链接重复提取时出现错误 91



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

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

当我有一个不同日期的列表并将函数复制粘贴到整个列表中时,我收到错误 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(https://www.dropbox.com/s/dg2j6o4xjr9v488/FX%20Rate%20Extraction%20Error%20%28stackoverflow%29.xlsx?dl=0(上创建了这个带有列表日期的小Excel文件。第一个是使用此函数完成的,应该可以轻松提取速率,没有任何错误。将公式复制粘贴到所有其他日期后,将出现错误 91。

>错误 91 表示未设置对象。

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

将错误处理放在无法获取xDoc以及随后无法设置xParentgetRateChild周围,它应该像一个魅力一样工作。

最新更新