1

first off, I have to admit I'm not very good at VBA. I've tried to adapt the code from this and this site to download the information I need on a list of given stock tickers. I have a list of the tickers in column A of sheet "data" and want the downloaded infos (name, exchange, bid, ask, etc.) in the columns to the right, starting in column c. I want to run the macro (and thus update all values) with a click on a button. I tried to adapt the code accordingly but keep on running into errors I cannot debug. Can you experts help me get the code right?

Thanks so much in advance!

Error


Sub DownloadStockQuotes(ByVal stockTicker As String, ByVal DestinationCell As String)

    Dim qurl As String
    Dim StartMonth, StartDay, StartYear, EndMonth, EndDay, EndYear As String
    Dim C As WorkbookConnection
    StartMonth = Format(Month(StartDate) - 1, "00")
    StartDay = Format(Day(StartDate), "00")
    StartYear = Format(Year(StartDate), "00")

    EndMonth = Format(Month(EndDate) - 1, "00")
    EndDay = Format(Day(EndDate), "00")
    EndYear = Format(Year(EndDate), "00")
    qurl = "URL;http://finance.yahoo.com/d/quotes.csv?s=" + stockTicker + "&f=nxj1b4abc1p2"

    On Error GoTo ErrorHandler:
    With ActiveSheet.QueryTables.Add(Connection:=qurl, Destination:=Range(DestinationCell))
        .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
ErrorHandler:

End Sub

Sub DownloadData()

    Dim frequency As String
    Dim numRows As Integer
    Dim lastRow As Integer
    Dim lastErrorRow As Integer
    Dim lastSuccessRow As Integer
    Dim stockTicker As String

    Application.ScreenUpdating = False

    lastRow = Worksheets("Kursabruf").Cells(Rows.Count, "a").End(xlUp).Row

    'Loop through all tickers
    For ticker = 2 To lastRow

        stockTicker = Worksheets("Kursabruf").Range("$a$" & ticker)

        If stockTicker = "" Then
            GoTo NextIteration
        End If

        Call DownloadStockQuotes(stockTicker, "$c$2")
        Worksheets("Kursabruf").Columns("c:c").TextToColumns Destination:=Range("c2"), DataType:=xlDelimited, _
                                     TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
                                     DecimalSeparator:=".", ThousandsSeparator:=" ", _
                                     Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1))

        Sheets(stockTicker).Columns("A:G").ColumnWidth = 10

        lastRow = Sheets(stockTicker).UsedRange.Row - 2 + Sheets(stockTicker).UsedRange.Rows.Count

            GoTo NextIteration

        'Delete final blank row otherwise will get ,,,, at bottom of CSV
        Sheets("Kursabruf").Rows(lastRow + 1 & ":" & Sheets("Kursabruf").Rows.Count).Delete


NextIteration:
    Next ticker

    Application.DisplayAlerts = False


ErrorHandler:

    Worksheets("Parameters").Select
    For Each C In ThisWorkbook.Connections
        C.Delete
    Next

End Sub
Error1000
  • 21
  • 3
  • 1
    If you can detail the errors you are getting, we will help you debug the code – Dave Jun 10 '16 at 14:04
  • Doesn't yahoo lets you download data to excel or did they remove that? – findwindow Jun 10 '16 at 16:17
  • Hi, thanks for your offer to help! Right now it's saying destination reference is not valid in the field 'Worksheets("Kursabruf").Columns("c:c").TextToColumns ...' - right after the Call of the stock download function. Regarding your question, @findwindow: Do you mean download in another way than via VBA? Please explain! :) Thanks again! – Error1000 Jun 10 '16 at 21:20
  • It's been years but last time I used it, yahoo has a link to download data direct to excel. Guessing it's not there anymore? Have you tried google finance or morningstar? Edit: also, most people don't pull stock data. If you are, you should have access to bloomberg or similar. – findwindow Jun 10 '16 at 21:24
  • Ah! MSN service is available, but I like the flexibility of the Yahoo API. I can chose from dozens of different values and customize my request. The one thing I can't seem to get done myself is how I get the result of my request URL into my excel file. CSV export is easy (explained in that first link I posted). – Error1000 Jun 10 '16 at 21:28
  • Too lazy to read the links XD Try adding the worksheet before `Range("c2")` – findwindow Jun 10 '16 at 21:32
  • Doesn't change the problem, unfortunately. :( – Error1000 Jun 10 '16 at 21:36
  • Too lazy to read the documentation XD Doesn't seem to matter if the ranges are equal... Edit: does [this](http://stackoverflow.com/questions/12431231/convert-text-to-columns-in-excel-using-vba) help? – findwindow Jun 10 '16 at 21:50

0 Answers0