好文分享

【EXCEL VBA】自動下載合併財務報表(BS、IS、CF)

Option Explicit
Sub 下載合併三大表()
Dim URL As String, xCo_Id As String, xSyear As String, xSseason As String

xCo_Id = “[” & “””股票代號””” & “,” & “””股票代號””” & “]” ‘要求輸入網頁的參數:股票代號
xSyear = “[” & “””年度””” & “,” & “””” & Format(Date, “e”) & “””” & “]” ‘Format(Date, “e”)->中華民國的年度
xSseason = “[” & “””季別””” & “,” & “””” & Format(Date, “q”) & “””” & “]” ‘Format(Date, “q”)->當年度的季別
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:=Range(“A1”))
.AdjustColumnWidth = True ‘自動調整欄寬
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = “2,3,4” ‘合併資產負債表,合併綜合損益表,合併現金流量表
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

End Sub