Why VBA cannot download CSV file from URL?

alee001

Board Regular
Joined
Sep 9, 2014
Messages
154
Office Version
  1. 2010
Platform
  1. Windows
I revised the URL as follow so that download the CSV file from Yahoo Finance history data, but there are occur error 1004 after "End With". Anyone solve it? Thank...
Code:
Sub GetStock(ByVal stockSymbol As String, ByVal StartDate As String,  _
ByVal EndDate As String, ByVal Kbar As String)
'Get and update price data from Yahoo
Dim DownloadURL As String
Dim StartPeriod As String, EndPeriod As String
Dim s As Integer, r As Integer, r0 As Integer
Dim chk As AutoFilter

'clear filter and old data
Set chk = dat.AutoFilter
If Not chk Is Nothing Then
    chk.Range.AutoFilter
End If
Range("A:G").ClearContents

StartPeriod = 946656000 + DateDiff("d", "2000/01/01", StartDate) * 86400
EndPeriod = 946656000 + DateDiff("d", "2000/01/01", EndDate) * 86400

DownloadURL = "URL;https://query1.finance.yahoo.com/v7/finance/download/"  _
+ stockSymbol + "?period1=" + StartPeriod + "&period2=" + EndPeriod  _
+ "&interval=1" + Kbar + "&events=history&crumb=NVyiH1kN4FA"
Range("k1").value = DownloadURL
With ActiveSheet.QueryTables.Add(Connection:=DownloadURL, Destination:= _
Range("$A$1"))
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .WebSelectionType = xlSpecifiedTables
    .WebFormatting = xlWebFormattingNone
    .WebTables = "20"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .refresh BackgroundQuery:=False
End With
ActiveWindow.SmallScroll Down:=-12
Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=True, Space:=False, other:=False, FieldInfo _
    :=Array(Array(1, 4), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1),  _
    Array(6, 1), Array(7, 1))
Columns("A:F").EntireColumn.AutoFit

Set chk = Nothing
    
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

Forum statistics

Threads
1,214,853
Messages
6,121,935
Members
449,056
Latest member
denissimo

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