CSV from Website CreateObject("Microsoft.XMLHTTP")

johnbird1988

Board Regular
Joined
Oct 6, 2009
Messages
199
Hello,

I have the below bit of code that will connect to a website download the .csv file into a location. At the moment I have set this on a loop of about ten files and save them in a location.

I then copy all the data from the ten files download into one master worksheet.

What I would like help with if possible is to not save the downloaded files but to copy and append them into a master worksheet either in one go or ten appended files.

Any help would be greatly appreciated, I have been searching the net for hours.

Code:
Dim WinHttpReq As Object
Dim myURL As String, sFilename As String
Dim c As Range

Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")

For Each c In Sheet1.Range("A1:A10")

sFilename = Environ("SystemDrive") & Environ("HomePath") & Application.PathSeparator & "Desktop" & Application.PathSeparator & c.value & ".csv"

myURL = "http://www.website.co.uk/index/test/" & c.value &".csv"

WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send

myURL = WinHttpReq.ResponseBody
If WinHttpReq.Status = 200 Then
    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.ResponseBody
    oStream.SaveToFile sFilename, 2
    oStream.Close
    
End If

Next c

Thank you

John
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this, using a different technique. The URLs are in cells A2:A11 on the first sheet and the CSV file contents are imported into the second sheet.
Code:
Public Sub Import_CSV_Files2()
    
    Dim URLcell As Range
    Dim destCell As Range, CSVstartRow As Long
    
    For Each URLcell In Worksheets(1).Range("A2:A11")
    
        With Worksheets(2)
        
            CSVstartRow = 1
            Set destCell = .Cells(Rows.Count, 1).End(xlUp)
            If destCell.Row > 1 Then
                Set destCell = destCell.Offset(1, 0)
                CSVstartRow = 2
            End If
    
            With .QueryTables.Add(Connection:="TEXT;" & URLcell.Value, Destination:=destCell)
                .TextFileStartRow = CSVstartRow
                .TextFileParseType = xlDelimited
                .TextFileCommaDelimiter = True
                .Refresh BackgroundQuery:=False
            End With
    
            .QueryTables(1).Delete
        End With
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,750
Members
448,989
Latest member
mariah3

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