我的函数从外部网站提取汇率。
我可以提取特定日期的单一费率。
当我有一个不同日期的列表并将函数复制粘贴到整个列表时,我收到错误 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。