I have tried the below code to import csv file to an excel sheet (Courtesty:http://investexcel.net/download-finviz-data/) and it is working fine. After importing the data, data type was not proper. Please see the screenshot.
The zero prefix was removed 2nd column after importing into excel. Is there any property like '.TextFileColumnDataTypes' for QueryTables.Add(Connection:="URL;"... ?
Sub GetWebCsvData()
Dim str As String
Dim myarray() As Variant
'Delete existing data
Sheets("Data").Activate 'Name of sheet the data will be downloaded into. Change as required.
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
str = "http://somedomain.com/filename.csv"
QueryQuote:
With Sheets("Data").QueryTables.Add(Connection:="URL;" & str, Destination:=Sheets("Data").Range("a1"))
.BackgroundQuery = True
.TablesOnlyFromHTML = false
.Refresh BackgroundQuery:=False
.SaveData = True
End With
Sheets("Data").Range("a1").CurrentRegion.TextToColumns Destination:=Sheets("Data").Range("a1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, other:=True, OtherChar:=",", FieldInfo:=Array(1, 2)
Sheets("Data").Columns("A:B").ColumnWidth = 12
Range("A1").Select
End Sub

