Dim URL As String, xCo_Id As String, x As Integer, Rng As Range
Dim E As Variant, xSyear As Integer, xSseason As Integer, D_Name As String
Dim Ia As Integer
With ActiveSheet
For Each E In .QueryTables 'WEB查詢物件集合
E.Delete
Next
For Each E In .Names 'Name 物件的集合
.Names(E.Name).Delete
Next
.UsedRange.Clear
Set Rng = .Range("a1") '指定工作表上 WEB查詢的位置
End With
xCo_Id = Application.InputBox("請輸入股票代號", , 2303) '預設為 2303
x = Year(Date) - 1910 '中華民國的年度
For xSyear = x To x - 3 Step -1 '迴圈:年度 '105->102
'For xSyear = X - 3 To X '迴圈:年度 '102->105
For xSseason = 4 To 1 Step -1 ' '迴圈:季別 '1,2,3,4
URL = "URL;http://mops.twse.com.tw/server-java/t164sb01?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Rng)
.Name = xCo_Id & "_" & xSyear & "_第" & xSseason & "季" 'WEB查詢的名稱
.AdjustColumnWidth = True '自動調整欄寬
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "2,3,4" '資產負債表,綜合損益表,現金流量表
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
If .ResultRange.Rows.Count = 2 Then '無資料
D_Name = .Name 'WEB查詢的名稱
.Delete '刪除:WEB查詢
With Rng.Parent
For Each E In .Names
If InStr(E.Name, D_Name) Then E.Delete '刪除:工作表上的名稱->WEB查詢的名稱
Next
End With
Else
With .ResultRange 'WEB查詢資料的範圍
Set Rng = .Cells(1, .Columns.Count + 2) '下一WEB查詢的位置
End With
End If
End With
Next
Next
Dim Ba As Integer '因讀取到還未發佈的財報,會留空白欄
'判斷前50欄是否有空白欄,有則刪除
For Ba = 1 To 50
If Range("A" & Ba).Value = "" Then
Selection.EntireColumn.Delete
Else
End If
Next