Results 1 to 3 of 3

QueryTables bug. If one query fails, all subsequent queries will fail unless restart Excel

Threaded View

  1. #1
    Registered User
    Join Date
    06-27-2024
    Location
    San Jose, California
    MS-Off Ver
    MS Office 10, MS Office 21
    Posts
    2

    QueryTables bug. If one query fails, all subsequent queries will fail unless restart Excel

    I wrote a VBA code to download stock data from Yahoo Finance. To explain the bug that I discovered, I wrote the following VBA program. The VBA program downloads data for stock symbols SPY, XXX, and QQQ from the Yahoo Finance website and displays the downloaded data on sheet1. Because the stock symbol XXX does not exist, the QueryTables to download the data of Stock XXX will generate an error. I used "On Error Resume Next" to ignore the error and move to the next line of the program to download data for stock QQQ. For Excel 2010, if the query to download data of a stock fails because the Yahoo Finance web server does not have the data., the next downloads for other stocks can still work. But , for Excel 2019 and Excel 2021. maybe other Excel of newer versions too, if a query for a stock fails, all next queries for other stocks will fail unless I restart Excel. I tested the problem in Windows 10 and 11, and the results are the same. Do any people know why new versions of Excel have this problem while Excel 2010 does not? I would appreciate it if someone could help me to solve this problem.

    I need to download data for over a hundred stocks. Data for some stock symbols are sometimes missing in Yahoo Finance. Because of the bug, if the download for one stock ticker fails, I have to restart Excel and download the data from the beginning again.

    ************************************************************
        
    
    Private Sub Download_From_Yahoo_Click()
    
      On Error Resume Next
      Dim web_Link         As String
           
    
      'Download Data for Stock Symbol SPY
      ' Please add h  t  t  p  :  / /  to the inside of the quotation mark in the following line because I am not allowed to post weblinks by this forum.
       web_Link =  "query1.finance.yahoo.com/v7/finance/download/SPY?period1=1687881429&period2=1719503829&interval=1d&events=history&includeAdjustedClose=true"    
        
        With Sheet1.QueryTables.Add(Connection:="TEXT;" & web_Link, _
                                                          Destination:=Sheet1.Range("A1"))
            .Name = "import_1"
            .FieldNames = True ' field names in source data appear as column headers
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlOverwriteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 1252
            .TextFileStartRow = 1               ' Start data from row 2 to exclude headings
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        
        
        
    
        
      ' Download Data for Stock Symobol XXX
    ' Please add h  t  t  p  :  / /  to the inside of the quotation mark in the following line because I am not allowed to post weblinks by this forum.
       web_Link =  "query1.finance.yahoo.com/v7/finance/download/XXX?period1=1687881429&period2=1719503829&interval=1d&events=history&includeAdjustedClose=true"    
        With Sheet1.QueryTables.Add(Connection:="TEXT;" & web_Link, _
                                                          Destination:=Sheet1.Range("I1"))
            .Name = "import_2"
            .FieldNames = True ' field names in source data appear as column headers
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlOverwriteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 1252
            .TextFileStartRow = 1               ' Start data from row 2 to exclude headings
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        
        
      
    ' Download Data for Stock Symobol QQQ
    ' Please add h  t  t  p  :  / /  to the inside of the quotation mark in the following line because I am not allowed to post weblinks by this forum.
         web_Link =  "query1.finance.yahoo.com/v7/finance/download/QQQ?period1=1687881429&period2=1719503829&interval=1d&events=history&includeAdjustedClose=true"   
      
        
        With Sheet1.QueryTables.Add(Connection:="TEXT;" & web_Link, _
                                                          Destination:=Sheet1.Range("Q1"))
            .Name = "import_3"
            .FieldNames = True ' field names in source data appear as column headers
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlOverwriteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 1252
            .TextFileStartRow = 1               ' Start data from row 2 to exclude headings
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        
        
        
        
    End Sub
    Last edited by sheepmob; 06-27-2024 at 06:32 PM. Reason: Make Title clearer

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Updating array formula from excel 2019 to use LET in excel 2021
    By Marvo in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-31-2024, 09:47 AM
  2. Replies: 8
    Last Post: 12-05-2022, 08:38 PM
  3. Unable to configure Excel 2019 the way I used Excel 2010.
    By stlucia in forum Excel - New Users/Basics
    Replies: 5
    Last Post: 10-10-2020, 05:42 PM
  4. Macro filter date works in Excel 2010 and not in 2019
    By Berna11 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-28-2020, 07:11 PM
  5. [SOLVED] Excel 2019 workbook with slicer won't work in Excel 2010 - help please
    By Excel enthusiast in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 11-07-2019, 12:59 PM
  6. Replies: 0
    Last Post: 09-27-2019, 03:34 PM
  7. macro works in excel 2019 but not in 2010 this gives type mismatch error 13
    By Newbikonob1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-20-2019, 06:25 AM

Tags for this Thread

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