標題:
EXCEL網頁查詢如何避開不存在網頁
[打印本頁]
作者:
chwqk
時間:
2010-12-29 14:16
標題:
EXCEL網頁查詢如何避開不存在網頁
[attach]4254[/attach]
小弟每日用EXCEL網頁查詢在證交所公告區"減資公告"為每日查詢之一項
路徑:
http://www.twse.com.tw/docs1/data01/market/public_html/b9912.txt
Sub Macro1()
For I = 1 To 12
If I < 10 Then
M = "0" & I
Else: M = I
End If
RD = "99" & M
MsgBox RD
Sheets("減資查詢").Select
Range("A1").Select
With Selection.QueryTable
.Connection = _
"URL;http://www.twse.com.tw/docs1/data01/market/public_html/b" & RD & ".txt"
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
Next I
End Sub
但是證交所公告區關於"減資公告"採月份查詢方式
當每月初尚無資料時則會出現錯誤訊息而中斷查詢工作
例如今年(99)4,5月無資料
歷史查詢即會出錯而中斷
其實每月一開始尚無新資料時都會中斷!!
請問有方法讓當月尚無資料時 網路查詢能偵測到 而不會中斷其後工作方法嗎?
作者:
GBKEE
時間:
2011-5-1 20:43
回復
1#
chwqk
試試看
Sub Macro1()
On Error Resume Next
For I = 1 To 12
If I < 10 Then
M = "0" & I
Else: M = I
End If
RD = "99" & M
MsgBox RD
Sheets("減資查詢").Select
Range("A1").Select
With Selection.QueryTable
.Connection = _
"URL;http://www.twse.com.tw/docs1/data01/market/public_html/b" & RD & ".txt"
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
If Err > 0 Then
MsgBox "查不到 " & RD
Err.Clear
End If
Next I
End Sub
複製代碼
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)