我的功能從外部網站提取匯率。從外部鏈接重複提取時出現錯誤91
我可以提取特定日期的單一費率。
當我列出不同日期時,出現錯誤91,並將該功能複製粘貼到整個列表中。 (我告訴Excel爲每個特定日期應用此功能。)
這是我的代碼(xDoc對象創建方法的功勞歸於AnalystCave位於analystcave.com/vba-xml-working-xml-files/):
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錯誤。
@Chrotensie,謝謝你的評論。是的,如果我忽略日期並縮短網址,它仍然會檢索費率,正如您所說的那樣。問題在於它會一直檢索最新的一個(並且11.07.2017是中央銀行最新的一個)。爲了使它檢索任何以前的日期,我將需要使用原始鏈接。我不確定我是否真正理解了有關錯誤處理的建議(我不擅長XML;事實上,我只是VBA編程中的業餘愛好者),並且我不確定這個建議是否適用,因爲我繼續使用原始網址。 –
@Chrotensie,另外,您繼續獲取11.07.2017而不是14.01.2017的XML的原因是該日期的格式應該是正確的。如果你注意到,我在代碼'corrDate'中有一個特殊的代碼行,然後在'xDoc.Load'中跟着一個正斜槓。 –