+ Reply to Thread
Results 1 to 6 of 6

Macro quitting before completion

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Macro quitting before completion

    I am running a macro that grabs info from Yahoo. The macro is working fine except when it encounters two particular situations.

    The macro is designed to grab a closing stock price for a date specified for a list of Symbols in column A and the date in column B. The closing price is then displayed in column D.

    The problems (there are two that I found) I am running into is that when the Macro encounters a symbol in Column A and is not recognized, an error message pops up and the macro stops altogether. I want the macro to continue to run down the list but I can't figure out where in the macro it is stopping and, more importantly, how to make it continue. Any help is appreciated. I have attached a copy of file if needed (if opened, you can see it stops working after row 11). Thanks

    The other issue, which is related to the first issue somewhat is if the macro runs into an invalid date in column B (e.g. the user may enter a date that is for a Saturday or Sunday in which Yahoo would not have any data). This also causes the macro to stop.

    Private Sub CommandButton1_Click()
    
        Application.ScreenUpdating = False
        
        Dim HSDFY As HistoricalStockDataFromYahoo
        Dim rs As ADODB.Recordset
        Dim i As Long
        Dim j As Integer
        Dim lastrow As Integer
    
        On Error GoTo Err_CommandButton1_Click
        
        Range("C2:D" & Rows.Count).ClearContents
    
        i = 2
    
        lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
        For j = 2 To lastrow
            Set HSDFY = New HistoricalStockDataFromYahoo
            Set rs = HSDFY.GetHistoricalData(Cells(j, 1).Value, Cells(j, 2).Value, Cells(j, 2).Value)
            rs.MoveFirst
            Cells(j, 3).CopyFromRecordset rs
            i = i + 1
        Next j
    
    Exit Sub
    
    Err_CommandButton1_Click:
        Select Case Err.Number
            Case 10000
                MsgBox Err.Description
            Case 10001
                'invalid interval
                MsgBox Err.Description
            Case 10002
                'query failed
                MsgBox Err.Description
        End Select
        
        Application.ScreenUpdating = True
        
    End Sub
    Option Explicit
    
    'Grabs Yahoo historical stock data
    'requires Microsoft ActiveX Data Objects 2.6 or later
    Private pWinHttpRequest As WinHttp.WinHttpRequest
    
    Friend Function GetHistoricalData(Symbol As String, _
        Optional FromDate As Date = #12:00:00 AM#, _
        Optional ToDate As Date = #12:00:00 AM#, _
        Optional Interval As String = "Daily") As ADODB.Recordset
        
        Dim URL As String, ResponseText As String
        Dim pRecordSet As ADODB.Recordset
        Dim DateString As String, IntervalString As String
        Dim RTS() As String, RTFI
        Dim x As Long
       
        If FromDate <> #12:00:00 AM# Or ToDate <> #12:00:00 AM# Then
            If FromDate = 0 And ToDate > 0 Then
                FromDate = #1/1/1900#
            ElseIf FromDate > 0 And ToDate = 0 Then
                ToDate = Date
            End If
            DateString = "&a=" & Format(Month(FromDate) - 1, "00") & "&b=" _
                            & Format(FromDate, "DD") & "&c=" & Format(FromDate, "YYYY") & _
                         "&d=" & Format(Month(ToDate) - 1, "00") & "&e=" _
                            & Format(ToDate, "DD") & "&f=" & Format(ToDate, "YYYY")
        End If
    
        
        URL = "http://ichart.finance.yahoo.com/table.csv?s=" & Symbol & DateString & IntervalString
        
        pWinHttpRequest.Open "GET", URL, False
        pWinHttpRequest.Send
         
        ResponseText = pWinHttpRequest.ResponseText
        If InStr(ResponseText, "<title>Yahoo! - 404 Not Found</title>") Then
                Err.Raise 10002, "HistoricalStockDataFromYahoo.GetHistoricalData", _
                "Invalid Search Parameters or other error.  No data was returned."
        End If
        
        Set pRecordSet = New ADODB.Recordset
        
        pRecordSet.Fields.Append "Date", adDBDate
        pRecordSet.Fields.Append "Close", adCurrency
        pRecordSet.Open
        
        RTS = Split(ResponseText, Chr(10))
        
        For x = LBound(RTS) + 1 To UBound(RTS)
            If RTS(x) <> "" Then
                RTFI = Split(RTS(x), ",")
                pRecordSet.AddNew Array("Date", "Close"), _
                Array(RTFI(0), RTFI(4))
                pRecordSet.Update
            End If
        Next x
    
        pRecordSet.MoveFirst
        Set GetHistoricalData = pRecordSet
    End Function
    
    Private Sub Class_Initialize()
        On Error Resume Next
        Set pWinHttpRequest = New WinHttpRequest
        If pWinHttpRequest Is Nothing Then
            Err.Raise 10000, "HistoricalStockDataFromYahoo.Class_Initialize", _
            "Could not create WinHttp.WinHttpRequest object..."
        End If
    End Sub
    Attached Files Attached Files
    Last edited by maacmaac; 11-25-2008 at 04:30 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Put Resume at the end of your error handler.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176
    I put resume (plus some additional code) in the error handler and that cleared up the first issue if symbol is not found in column A. Still having trouble if the user enters and invalid date in column B (e.g. enters a Saturday, Sunday, holiday, etc). Not sure if I need another resume someplace in the code and I am just not seeing it.

    Option Explicit
    
    Private Sub CommandButton1_Click()
    
        Application.ScreenUpdating = False
        
        Dim HSDFY As HistoricalStockDataFromYahoo
        Dim rs As ADODB.Recordset
        Dim i As Long
        Dim j As Integer
        Dim lastrow As Integer
    
        On Error GoTo Err_CommandButton1_Click
        
        Range("C2:D" & Rows.Count).ClearContents
    
        i = 2
    
        lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
        For j = 2 To lastrow
            Set HSDFY = New HistoricalStockDataFromYahoo
            Set rs = HSDFY.GetHistoricalData(Cells(j, 1).Value, Cells(j, 2).Value, Cells(j, 2).Value)
            rs.MoveFirst
            Cells(j, 3).CopyFromRecordset rs
            i = i + 1
        Next j
    
    Exit Sub
    
    Err_CommandButton1_Click:
        Select Case Err.Number
            Case 10000
                MsgBox Err.Description
                j = j + 1
                i = i + 1
                Resume
            Case 10001
                'invalid interval
                MsgBox Err.Description
                j = j + 1
                i = i + 1
                Resume
            Case 10002
                'query failed
                MsgBox Err.Description
                j = j + 1
                i = i + 1
                Resume
        End Select
        
        Application.ScreenUpdating = True
        
    End Sub

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Set breakpoints; what if the error is none of those you catch?

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Err_CommandButton1_Click:
        Select Case Err.Number
            Case 10000
                MsgBox Err.Description
                j = j + 1
                i = i + 1
            Case 10001
                'invalid interval
                MsgBox Err.Description
                j = j + 1
                i = i + 1
            Case 10002
                'query failed
                MsgBox Err.Description
                j = j + 1
                i = i + 1
           Case Else
                MsgBox "some other error"
        End Select
        Resume
        
    End Sub

  6. #6
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176
    That did the trick. Code is working as expected. As always, thank you and all others that take the time out of their day to help others with Excel issues.

    P.S. I really liked your advice for setting breakpoints. This was something I did not know even existed before you made your comment. This is really helpful for debugging.

    Err_CommandButton1_Click:
        Select Case Err.Number
            Case 10000
                MsgBox Err.Description
                j = j + 1
                i = i + 1
            Case 10001
                'invalid interval
                MsgBox Err.Description
                j = j + 1
                i = i + 1
            Case 10002
                'query failed
                MsgBox Err.Description
                j = j + 1
                i = i + 1
           Case Else
                MsgBox "Invalid Search Parameters or other error.  No data was returned."
                j = j + 1
                i = i + 1
        End Select
        Resume

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1