iT邦幫忙

1

VBA 使用Power Query抓取網站資料語法問題

  • 分享至 

  • twitterImage

各位專家好
小弟目前初學VBA,透過"巨集+改寫"目的是抓取網站的股票收盤資料
已經將網站變數定義好,但由於使用的是office365,所以下載網站資料的程式碼和大部分網路上查得有點不同(似乎是 Power Query 的語言),大概知道是語法沒辦法抓到我定義的網址,但不知道如何修改,想請專家們給予指導,感激不敬!

Sub GETSTOCK()

Dim StrStockNo As String
Dim StrStartDate As String
Dim StrEndDate As String
Dim StrURL As String
Dim StrURLTemp As String
Dim StrStockName As String

Call DelAllSheet
Sheets("練習").Select
Range("A2").Select


StrStartDate = Range("H2").Value
StrEndDate = Range("I2").Value
StrURL = Range("F16").Value
StrURLTemp = Replace(Replace(StrURL, "[startdate]", StrStartDate), "[enddate]", StrEndDate)

StrStockNo = ActiveCell.Value
StrStockName = ActiveCell.Offset(0, 1).Value

    Sheets("StockNoTemplate").Select
    Sheets("StockNoTemplate").Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = StrStockName & StrStockNo
    Range("L3").Select
    
    StrURL = Replace(StrURLTemp, "[stockno]", StrStockNo)
    ActiveWorkbook.Queries.Add Name:="Table 0 (11)", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    來源 = Web.Page(Web.Contents(& StrURL))," & Chr(13) & "" & Chr(10) & "    Data0 = 來源{0}[Data]," & Chr(13) & "" & Chr(10) & "    已變更類型 = Table.TransformColumnTypes(Data0,{{""日期"", type date}, {""開盤"", type number}, {""最高"", type number}, {""最低"", type number}, {""收盤"", " & _
        "type number}, {""漲跌"", type number}, {""漲%"", Percentage.Type}, {""成交量"", Int64.Type}, {""成交金額"", Int64.Type}, {""本益比"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    已變更類型" & _
        ""
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 0 (11)"";Extended Properties=""""" _
        , Destination:=Range("$L$3")).QueryTable
        .CommandType = xlCmdSql
        .CommandText = Array("SELECT * FROM [Table 0 (11)]")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_0__11"
        .Refresh BackgroundQuery:=False
    End With

主要的問題在於

ActiveWorkbook.Queries.Add Name:="Table 0 (11)", Formula:= _
        "let" & Chr(13) & "" & Chr(10) & "    來源 = Web.Page(Web.Contents(& StrURL))," & Chr(13) & "" & Chr(10) & "    Data0 = 來源{0}[Data]," & Chr(13) & "" & Chr(10) & "    已變更類型 = Table.TransformColumnTypes(Data0,{{""日期"", type date}, {""開盤"", type number}, {""最高"", type number}, {""最低"", type number}, {""收盤"", " & _
        "type number}, {""漲跌"", type number}, {""漲%"", Percentage.Type}, {""成交量"", Int64.Type}, {""成交金額"", Int64.Type}, {""本益比"", type number}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & "    已變更類型" & _
        ""

Web.Page(Web.Contents())這個語法的使用上,網址以定義為StrURL,但目前目前的寫法在執行後卻視StrURL為單純的文字,而非以定義的網址,不知道該怎麼修改才能讓語法抓到我定義的StrURL。
感謝~

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
海綿寶寶
iT邦大神 1 級 ‧ 2021-03-22 16:02:41
最佳解答
"    來源 = Web.Page(Web.Contents(& StrURL))," & Chr(13) & "" & Chr(10)

改成

"    來源 = Web.Page(Web.Contents(" & StrURL & "))," & Chr(13) & "" & Chr(10)

試試看

看更多先前的回應...收起先前的回應...

網址有抓到我定義的網址了,非常感謝!
但執行後還是顯示有錯誤。

.Refresh BackgroundQuery:=False

偵錯後,指出是這個程式有問題。
我把連線的資料打開後顯示如下:

= let
    來源 = Web.Page(Web.Contents(https://www.cnyes.com/twstock/ps_historyprice.aspx?code=8422&ctl00$ContentPlaceHolder1$startText=2015/12/01&ctl00$ContentPlaceHolder1$endText=2015/12/31)),
    Data0 = 來源{0}[Data],
    已變更類型 = Table.TransformColumnTypes(Data0,{{"日期", type date}, {"開盤", type number}, {"最高", type number}, {"最低", type number}, {"收盤", type number}, {"漲跌", type number}, {"漲%", Percentage.Type}, {"成交量", Int64.Type}, {"成交金額", Int64.Type}, {"本益比", type number}})
in
    已變更類型

https://ithelp.ithome.com.tw/upload/images/20210322/20136140YXvVg3A2RN.png

偵錯上顯示是應用程式或物件定義上的錯誤@@
想請問這是上面程式還有什麼問題嗎?

再加括號試試看

"    來源 = Web.Page(Web.Contents(""" & StrURL & """))," & Chr(13) & "" & Chr(10)

沒問題了!非常感謝您!

問題解決就好
有明牌可以分享更好
/images/emoticon/emoticon44.gif

這個跟程式一樣還在學習XD

我要發表回答

立即登入回答