返回列表 上一主題 發帖

[發問] 擷取上櫃股價行情~遇到非交易日即跳出錯誤訊息!

[發問] 擷取上櫃股價行情~遇到非交易日即跳出錯誤訊息!

各位大大:

再次請教...
我抓上櫃連續多日的收盤行情時,
遇到假日(非交易日)時,vba都會出現以下訊息~~執行階段錯誤'1004'-無法開啟連結...


該網頁(上櫃每日收盤行情有關的原始碼如下


請問該怎麼修改以下的vba代碼,
使其可以先判定當日有沒有交易資料,再進行資料擷取/或跳至下一個交易日 ???

(抓上市收盤價時就算遇到假日也不會出現此一問題,但抓上櫃收盤價時,則會有錯誤訊息...)

感謝!!!
  1. '上櫃個股歷史資料
  2.    With ActiveSheet.QueryTables.Add(Connection:="URL;http://www.otc.org.tw/ch/stock/aftertrading/otc_quotes_no1430/SQUOTE_AL_" & myear & mmon & nowday & ".html" _
  3.         , Destination:=ActiveCell.Offset(2, 0))
  4.    
  5.      
  6.         .FieldNames = True
  7.         .RowNumbers = False
  8.         .FillAdjacentFormulas = True
  9.         .PreserveFormatting = True
  10.         .RefreshOnFileOpen = False
  11.         .BackgroundQuery = True
  12.         .RefreshStyle = xlInsertDeleteCells
  13.         .SavePassword = False
  14.         .SaveData = False
  15.         .AdjustColumnWidth = True
  16.         .RefreshPeriod = 0
  17.         .WebSelectionType = xlSpecifiedTables
  18.         .WebFormatting = xlWebFormattingNone
  19.         .WebTables = "1,2,3"
  20.         .WebPreFormattedTextToColumns = True
  21.         .WebConsecutiveDelimitersAsOne = True
  22.         .WebSingleBlockTextImport = False
  23.         .WebDisableDateRecognition = False
  24.         .WebDisableRedirections = False
  25.         .Refresh BackgroundQuery:=False
  26.     End With
  27.    
複製代碼
新手上路

回復 1# cudui


錯誤訊息都在~程式run到 .refresh BackgroundQuery:=False 時出現error(執行階段錯誤1004)
新手上路

TOP

回復 1# cudui


    請問該怎麼修改以下的vba代碼,
使其可以先判定當日有沒有交易資料,再進行資料擷取/或跳至下一個交易日 ???

   你的問題很籠統...沒法完整回答......如果是一個可正確run的vba,也要先說明你的個邏輯...

    最簡單的方法是將整年的交易日期計劃拿到,比對欲抓取的日期目標,依判定變更report的html.....

TOP

http://www.otc.org.tw/ch/stock/aftertrading/otc_quotes_no1430/SQUOTE_AL_1010419.CSV
你又抓到沒有資料的網頁了....

TOP

''上櫃個股歷史資料
   With ActiveSheet.QueryTables.Add(Connection:="URL;http://www.otc.org.tw/ch/stock/aftertrading/otc_quotes_no1430/SQUOTE_AL_" & myear & mmon & nowday & ".CSV" _
        , Destination:=ActiveCell.Offset(2, 0))
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = True
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = False
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "1,2,3"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
    End With
   
   
    Range("a3:a2200").Select
       Selection.TextToColumns Destination:=Range("b4"), DataType:=xlDelimited, _
       TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
       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), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), _
         Array(14, 1), Array(15, 1)), TrailingMinusNumbers:=True
    Range("a4:a2200").Delete
    Rows("4:4").Select
    Selection.Delete Shift:=xlUp
    Columns("A:O").Select
    Range("a1").Activate
    Selection.ColumnWidth = 8
   
End Sub

試試看吧

TOP

''上櫃個股歷史資料
   With ActiveSheet.QueryTables.Add(Connection:="URL;http://www.otc.org.tw/ch/st ...
chen_cook 發表於 2012-4-20 07:14



    這是錯誤的資料

TOP

回復 2# cudui
  1. On Error Resume Next
  2.     With ActiveSheet.QueryTables.Add(Connection:="URL;http://www.otc.org.tw/ch/stock/aftertrading/otc_quotes_no1430/SQUOTE_AL_" & myear & mmon & nowday & ".html" _
  3.         , Destination:=ActiveCell.Offset(2, 0))
  4.         .WebSelectionType = xlEntirePage
  5.         .WebFormatting = xlWebFormattingNone
  6.         .WebPreFormattedTextToColumns = True
  7.         .WebConsecutiveDelimitersAsOne = True
  8.         .WebSingleBlockTextImport = False
  9.         .WebDisableDateRecognition = False
  10.         .WebDisableRedirections = False
  11.         .Refresh BackgroundQuery:=False
  12.         If Err.Number <> 0 Then Err.Clear: MsgBox "資料查詢失敗"
  13.     End With
複製代碼

TOP

http://www.otc.org.tw/ch/stock/aftertrading/otc_quotes_no1430/SQUOTE_AL_1010419.CSV
你又抓到沒有資料的網頁了....


這網頁我確定是可以run得到資料下來的~~
交易日沒有問題,但是非交易時無data時才會出錯~


   你的問題很籠統...沒法完整回答......如果是一個可正確run的vba,也要先說明你的個邏輯...
    最簡單的方法是將整年的交易日期計劃拿到,比對欲抓取的日期目標,依判定變更report的html.....


交易日每年都不同,所以用比對的方法,會比較麻煩~
我的問題可能還不夠明確,
希望得到的解答是:如何在該vba內,添加判斷式---判斷若遇到"查詢失敗"或"查無資料"時,可以 goto NEXT one,繼續未完的查詢,而非跳出....
(因為不知道當 html無資料可擷取時,會回饋什麼樣的訊號...)
新手上路

TOP

回復 7# GBKEE
  1. If Err.Number <> 0 Then Err.Clear: MsgBox "資料查詢失敗"
複製代碼
請問G大, 該行可以改成下列寫法嗎?
If Err.Number <> 0 Then
GOTO ABC123
END IF
....

ABC123:
Next J ....



謝謝!!
新手上路

TOP

''上櫃個股歷史資料
   With ActiveSheet.QueryTables.Add(Connection:="URL;http://www.otc.org.tw/ch/st ...
chen_cook 發表於 2012-4-20 07:14


謝謝大大了,
我再試試CSV & Html執行有什麼差別~~
新手上路

TOP

        靜思自在 : 人要知福、惜福、再造福。
返回列表 上一主題