- 帖子
- 96
- 主題
- 18
- 精華
- 0
- 積分
- 125
- 點名
- 0
- 作業系統
- Windows 7
- 軟體版本
- Office 2007
- 閱讀權限
- 20
- 性別
- 男
- 註冊時間
- 2014-3-23
- 最後登錄
- 2022-8-2
|
回復 23# GBKEE
GBKEE版主您好,將您的程式碼套入之後是可以將1420跳過不抓資料了,不過因為1420也是在迴圈變數E的其中一碼,是不是無法用迴圈方式去避免抓取資料
只能一個一個像這樣[If E = 1420 Then GoTo xlNext]設定讓它跳過呢?因為像這種股票還真不少,要一個一個找出來可能要花些功夫
另外像這段[If InStr(.[A3], "查無") And Msg = True Or E = 2149 Then GoTo xlNext]當中的2149是代表什麼呢?我把Or E = 2149拿掉似乎不影響擷取資料
這個網站的資料出現"查無"是在A2儲存格,所以我把A3改成A2,附上程式碼,謝謝!- Option Explicit
- Sub 抓季月營收資料()
- Dim E As Integer, URL As String, xPath As String, xFile As String
- Dim Msg As Boolean
- URL = "URL;https://djinfo.cathaysec.com.tw/Z/ZC/ZCH/ZCH.DJHTM?A="
- xPath = "G:\財報資料"
- With ThisWorkbook
- With .Sheets(1) '活頁簿的第 1 張工作表
- If .QueryTables.Count = 0 Then
- With .QueryTables.Add(Connection:=URL, Destination:=.Range("$A$1"))
- .Refresh BackgroundQuery:=False
- End With
- End If
- Rows(1).Delete
- Columns(1).Delete
- For E = 1101 To 3000
- ER:
- With .QueryTables(1)
- .Connection = URL & E
- .PreserveFormatting = True
- .BackgroundQuery = True
- .RefreshStyle = xlInsertDeleteCells
- .SaveData = True
- .AdjustColumnWidth = True
- .RefreshPeriod = 0
- .WebSelectionType = xlSpecifiedTables
- .WebFormatting = xlWebFormattingNone
- .WebTables = "3"
- .WebPreFormattedTextToColumns = True
- .WebConsecutiveDelimitersAsOne = True
- .Refresh BackgroundQuery:=False
- End With
- If E = 1420 Then GoTo xlNext '加上試試看
- If InStr(.[A2], "查無") And Msg = True Then GoTo xlNext
- If InStr(.[A2], "查無") Then Msg = True: GoTo ER
- If InStr(.[A3], "個股代碼錯誤") = False Then '這網頁如股票代碼錯誤會傳回負號.
- xFile = xPath & "\" & E & "\REVENUE.txt"
- MkDir_Sub xFile '10#的程式 'C槽下的季損益表資料夾不需先建立
- Maketxt xFile, .QueryTables(1)
- End If
- xlNext:
- Msg = False
- Next
- End With
- End With
- End Sub
複製代碼 |
|