简体   繁体   中英

VBA Download CSV from Website

I am struggling with a VBA script which I have written. It is being called 4 times to download 4 different csv files from a Website. If I execute the script step by step by pressing F8 it works fine. However, when I am running the whole script it doesn't download all files. Sometimes it downloads one or two files and sometimes None- it's kind of unpredictable!? Has anybody an idea what wrong with the code of the Sub DownloadIshares()?

Many thanks in advance

Sub Test()
DownloadIshares "EUN5"
DownloadIshares "IBCS"
DownloadIshares "LQDH"
DownloadIshares "SDIG"
End Sub

Sub DownloadIshares(inETFName As String)

Dim o_IE As InternetExplorer
Dim FSO As FileSystemObject
Dim o_TextStream As TextStream
Dim HTMLDoc As HTMLDocument
Dim urlETF As String
Dim links As IHTMLElementCollection
Dim link As HTMLAnchorElement
Dim WB_tmp As Workbook
Dim MainPath As String, MainUrl_1 As String, MainUrl_2 As String, UrlETF_No As String
Dim SearchForLink As String

MainPath = "U:\Entwicklung\Instrumentenabgleich_ETF\"
MainUrl_1 = "https://www.ishares.com/de/professionelle-anleger/de/site-entry?siteEntryAction=ACCEPT&targetUrl" _
    & "=%2Fde%2Fprofessionelle-anleger%2Fde%2Fprodukte%2F"
MainUrl_2 = "%2F%3FsiteEntryPassthrough%3Dtrue%26refer" _
    & "rer%3DtickerSearch%26locale%3Dde_DE%26userType%3Dinstitutional"

Select Case inETFName
    Case "EUN5"
        UrlETF_No = "251726"
        SearchForLink = "_Datenblatt_GroMiKV_IE00BF11F565.csv"
    Case "LQDH"
        UrlETF_No = "257320"
        SearchForLink = "_Datenblatt_GroMiKV_IE00BCLWRB83.csv"
    Case "IBCS"
        UrlETF_No = "251565"
        SearchForLink = "_Datenblatt_GroMiKV_IE0032523478.csv"
    Case "SDIG"
        UrlETF_No = "258126"
        SearchForLink = "_Datenblatt_GroMiKV_IE00BYXYYP94.csv"
End Select

urlETF = MainUrl_1 & UrlETF_No & MainUrl_2
Set FSO = New FileSystemObject
Set o_IE = New InternetExplorer
o_IE.Visible = True
'o_IE.Visible = False

o_IE.Navigate urlETF
Do While o_IE.Busy Or o_IE.readyState <> 4
    DoEvents
Loop

Set HTMLDoc = o_IE.Document
Set links = HTMLDoc.getElementsByTagName("A")
For Each link In links
    'If InStr(link.href, "_Datenblatt_GroMiKV_IE00BCLWRB83.csv") Then
    If InStr(link.href, SearchForLink) > 0 Then
        Debug.Print "Text: " & link.innerText & vbCr & "URL: " & link.href
        'Set o_TextStream = FSO.OpenTextFile(link.href)
        Set WB_tmp = Workbooks.Open(link.href)
        WB_tmp.SaveAs MainPath & inETFName, xlCSV
        Application.DisplayAlerts = False
        WB_tmp.Close
        Application.DisplayAlerts = True
    End If
Next

End Sub

I think you can simplify your code a lot!

Sub OpenCSV()
strURL = "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv"
Application.Workbooks.Open (strURL)
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM