Hi
I have been trying to automate a csv table download into excel from a url (US Treasury) which works when done manually and was working for a while using the below VBA. Now however, it has decided that it doesn't want to recognise the comma delimiter and is dumping everything into just the one column. The code and url are included below and I would very much appreciate anyone who might point out where I may have erred, as I'm currently flummoxed on what I original thought was a relatively simple script!
Option Explicit
Sub GetDataTipsYields()
Dim Output_sheet_name As String
Dim Output_cell_address As String
Dim Clear_range As String
Dim qurl As String
Dim nQuery As Name
Dim LastRow As Integer
Output_sheet_name = Range("Output_sheet_query_2").Value ' variable sheet name
Output_cell_address = Range("Output_cell_query_2").Value ' variable output cell address
Clear_range = Range("Clear_cells_query_2").Value ' variable cell range to clear out old data
Sheets(Output_sheet_name).Select
Range(Clear_range).Select
Selection.ClearContents
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
qurl = Range("TipsYieldURL").Value ' a URL for the Treasury csv
QueryQuote:
With Sheets(Output_sheet_name).QueryTables.Add(Connection:="URL;" & qurl, Destination:=Sheets(Output_sheet_name).Range(Output_cell_address))
.BackgroundQuery = True
.TablesOnlyFromHTML = False
.Refresh BackgroundQuery:=False
.SaveData = True
End With
Sheets(Output_sheet_name).Range(Output_cell_address).CurrentRegion.TextToColumns Destination:=Sheets(Output_sheet_name).Range(Output_cell_address), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, other:=False ' delimiter set for comma separators
Sheets(Output_sheet_name).Columns("A:z").ColumnWidth = 12
LastRow = Sheets(Output_sheet_name).UsedRange.Row - 1 + Sheets(Output_sheet_name).UsedRange.Rows.Count
' ---------------- last part to sort data
Sheets(Output_sheet_name).Sort.SortFields.Add Key:=Range(Output_cell_address & ":A" & LastRow), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With Sheets(Output_sheet_name).Sort
.SetRange Range(Output_cell_address & ":z" & LastRow)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Bookmarks