【问题标题】:VBA QueryTables not working with Yahoo FinanceVBA QueryTables 不适用于雅虎财经
【发布时间】:2021-04-12 17:07:43
【问题描述】:

我正在尝试这个简单的 Sub(在 Excel 2019 中)从特定的 Yahoo Finance 页面导入表格。

它适用于其他网站,但使用 Yahoo Finance,我不断收到相同的消息“此 Web 查询未返回数据。要更改............”并且没有提取数据。

我不知道原因。

Sub ImportTable()

Dim ws As Worksheet
Dim qt As QueryTable
Dim qurl As String


qurl = "https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT"
Set ws = worksheets.Add    

Set qt = ws.QueryTables.Add( _
    Connection:="URL;" & qurl, _
    Destination:=Range("A1"))
    
With qt
    .RefreshOnFileOpen = False
    .Name = "test"
    .WebFormatting = xlWebFormattingNone
    .WebSelectionType = xlAllTables
    .Refresh
End With

End Sub

【问题讨论】:

    标签: excel vba yahoo-finance


    【解决方案1】:

    您可以改为设置与各种表的连接,将它们添加到数据模型中 (Alt + B + M)。在数据的查询窗格中,如果您希望查看它们,您可以选择将它们加载到工作簿的位置。您也可以在数据模型中使用它们并生成最终所需的输出。


    注意:

    更快的方法是,从功能区,data > from web > 输入 url > 选择感兴趣的表。这将在没有 vba 的情况下建立表连接。


    VBA:

    Option Explicit
    
    Public Sub AddYahooFinanceTableConnections()
        
        Dim wb As ThisWorkbook
        
        Set wb = ThisWorkbook
     
        wb.Queries.Add Name:="Balance Sheet", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & "    Data10 = Source{10}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data10,{{""Total Cash (mrq)"", type text}, {""131.99B"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        wb.Queries.Add Name:="Cash Flow Statement", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & "    Data11 = Source{11}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data11,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        wb.Queries.Add Name:="Dividends & Splits", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & "    Data5 = Source{5}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data5,{{""Forward Annual Dividend Rate"", type text}, {""2.24"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        wb.Queries.Add Name:="Fiscal Year", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & "    Data6 = Source{6}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data6,{{""Column1"", type text}, {""Column2"", type date}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        wb.Queries.Add Name:="Income Statement", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & "    Data9 = Source{9}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data9,{{""Revenue (ttm)"", type text}, {""153.28B"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        wb.Queries.Add Name:="People Also Watch", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & "    Data12 = Source{12}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data12,{{""Symbol"", type text}, {""Last Price"", type number}, {""Change"", type number}, {""% Change"", Percentage.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        wb.Queries.Add Name:="Profitability", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & "    Data7 = Source{7}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data7,{{""Column1"", type text}, {""Column2"", Percentage.Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        wb.Queries.Add Name:="Share Statistics", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & "    Data4 = Source{4}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data4,{{""Avg Vol (3 month)"", type text}, {""30.36M"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        wb.Queries.Add Name:="Stock Price History", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & "    Data3 = Source{3}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data3,{{""Beta (5Y Monthly)"", type text}, {""0.79"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        wb.Queries.Add Name:="Table 0", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & "    Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        wb.Queries.Add Name:="Table 1", Formula:= _
            "let" & Chr(13) & "" & Chr(10) & "    Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/MSFT/key-statistics?p=MSFT""))," & Chr(13) & "" & Chr(10) & "    Data1 = Source{1}[Data]," & Chr(13) & "" & Chr(10) & "    #""Changed Type"" = Table.TransformColumnTypes(Data1,{{""Column1"", type text}, {""Column2"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    #""Changed Type"""
        wb.Connections.Add2 "Query - Balance Sheet", _
            "Connection to the 'Balance Sheet' query in the workbook.", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Balance Sheet;Extended Properties=" _
            , """Balance Sheet""", 6, True, False
        wb.Connections.Add2 "Query - Cash Flow Statement", _
            "Connection to the 'Cash Flow Statement' query in the workbook.", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Cash Flow Statement;Extended Properties=" _
            , """Cash Flow Statement""", 6, True, False
        wb.Connections.Add2 "Query - Dividends & Splits", _
            "Connection to the 'Dividends & Splits' query in the workbook.", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Dividends & Splits;Extended Properties=" _
            , """Dividends & Splits""", 6, True, False
        wb.Connections.Add2 "Query - Fiscal Year", _
            "Connection to the 'Fiscal Year' query in the workbook.", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Fiscal Year;Extended Properties=" _
            , """Fiscal Year""", 6, True, False
        wb.Connections.Add2 "Query - Income Statement", _
            "Connection to the 'Income Statement' query in the workbook.", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Income Statement;Extended Properties=" _
            , """Income Statement""", 6, True, False
        wb.Connections.Add2 "Query - People Also Watch", _
            "Connection to the 'People Also Watch' query in the workbook.", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=People Also Watch;Extended Properties=" _
            , """People Also Watch""", 6, True, False
        wb.Connections.Add2 "Query - Profitability", _
            "Connection to the 'Profitability' query in the workbook.", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Profitability;Extended Properties=" _
            , """Profitability""", 6, True, False
        wb.Connections.Add2 "Query - Share Statistics", _
            "Connection to the 'Share Statistics' query in the workbook.", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Share Statistics;Extended Properties=" _
            , """Share Statistics""", 6, True, False
        wb.Connections.Add2 "Query - Stock Price History", _
            "Connection to the 'Stock Price History' query in the workbook.", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Stock Price History;Extended Properties=" _
            , """Stock Price History""", 6, True, False
        wb.Connections.Add2 "Query - Table 0", _
            "Connection to the 'Table 0' query in the workbook.", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table 0;Extended Properties=" _
            , """Table 0""", 6, True, False
        wb.Connections.Add2 "Query - Table 1", _
            "Connection to the 'Table 1' query in the workbook.", _
            "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Table 1;Extended Properties=" _
            , """Table 1""", 6, True, False
      
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      相关资源
      最近更新 更多