Runtime Error QueryTables to download Web CSV

CharlesB

New Member
Joined
Oct 23, 2014
Messages
4
I am having trouble downloading a csv file from the internet and importing it into an excel sheet. The method I am using is the Querytable.add method

This code works

Code:
Sub Import_CSV_File_From_URL()

    
    Dim URL As String
       
    Sheets("Sheet2").Select
    
    URL = "https://example.com/filename" & _
        Format(Range("TWDate"), "yyyymmdd")
     
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & URL, Destination:=Range("A1"))
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileCommaDelimiter = True
        .Refresh BackgroundQuery:=False
    End With
    
    ActiveSheet.QueryTables(1).Delete
    




 
End Sub

(url changed as it contains a auth key) - TWDate is a named cell which contains the date of the previous Sunday so that the url ends with the date to request the most recent weeks data.

But I am having difficulty with this code where I attempt to add 2 dates (so that the web generates a file which compares this weeks data with last week)

Code:
Sub Import_CSV_File_From_URL()


    
    Dim URL As String
       
    Sheets("Sheet2").Select
    
    URL = "https://example.com/filename" & _
        Format(Range("TWDate"), "yyyymmdd") & "&period_b=W" & Format(Range("LWDate"), "yyyymmdd")
     
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & URL, Destination:=Range("A1"))
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileCommaDelimiter = True
        .Refresh BackgroundQuery:=False
    End With
    
    ActiveSheet.QueryTables(1).Delete
    




 
End Sub

Using this code I get a Runtime Error 5 Invalid procedure call or argument - on the line beginning With ActiveSheet.QueryTables.Add. Any Idea why? if I add the line Range("a1").Value = URL to effectively print my url to a cell I can see that the URL is correct and downloads if I just paste it into the browser so why is the macro failing?

Thanks in advance
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top