標題:
[發問]
如何可以指定日期後下載資料
[打印本頁]
作者:
pupai
時間:
2013-12-3 10:30
標題:
如何可以指定日期後下載資料
您好
我希望程式可以依照我想要的日期下載當天資料
可以幫我指導修改
謝謝!!
Option Explicit
Sub EX()
Dim URL As String, xCo_Id As String, xSyear As String, xSseason As String
xCo_Id = Range("B1").Value
xSyear = Range("B2").Value
xSseason = Range("B3").Value
'xCo_Id = "[" & """年度""" & "," & """102年""" & "]"
'要求輸入網頁的參數:年度
'xSyear = "[" & """月份""" & "," & """月 """ & "]"
'Format(Date, "e")->中華民國的月份
'xSseason = "[" & """日""" & "," & """日期""" & "]"
'Format(Date, "q")->當年度的日期
'外資及陸資買賣超彙總表
URL = "URL;http://www.twse.com.tw/ch/trading/fund/TWT38U/TWT38U.php?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Range("D4"))
.AdjustColumnWidth = False '自動調整欄寬
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "2"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
'投信買賣超彙總表
URL = "URL;http://www.twse.com.tw/ch/trading/fund/TWT44U/TWT44U.php?step=1&CO_ID=" & xCo_Id & "&SYEAR=" & xSyear & "&SSEASON=" & xSseason & "&REPORT_ID=C"
With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=Range("K5"))
.AdjustColumnWidth = False '自動調整欄寬
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "2"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
複製代碼
作者:
GBKEE
時間:
2013-12-6 18:31
本帖最後由 GBKEE 於 2013-12-9 15:53 編輯
回復
1#
pupai
每一網頁的元素會有所不同.
Option Explicit
Sub 外資及陸資買賣超彙總表()
Dim URL As String, xDate As Date
xDate = Format(Date-1, "E/M/D")
URL = "URL;http://www.twse.com.tw/ch/trading/fund/TWT38U/TWT38U.php?qdate=" & xDate
With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=ActiveSheet.Range("a1"))
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "5"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
Sub 投信買賣超彙總表()
Dim URL As String, xDate As Date
xDate = Format(Date-1, "E/M/D")
URL = "URL;http://www.twse.com.tw/ch/trading/fund/TWT44U/TWT44U.php?qdate=" & xDate
With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=ActiveSheet.Range("D4"))
.Connection = URL
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "5"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
End Sub
複製代碼
作者:
pupai
時間:
2013-12-9 09:49
回復
2#
GBKEE
版主
請教為什麼程式跑不出東西
謝謝
作者:
GBKEE
時間:
2013-12-9 15:52
本帖最後由 GBKEE 於 2013-12-9 18:32 編輯
回復
3#
pupai
Option Explicit
Sub 外資及陸資買賣超彙總表()
Dim URL As String, xDate As Date
xDate = Date - 1 '經查: 外資及陸資買賣超彙總表 提供前一營業日的資料
ReDate:
URL = "URL;http://www.twse.com.tw/ch/trading/fund/TWT38U/TWT38U.php?qdate=" & Format(xDate, "E/M/D")
With ActiveSheet.QueryTables.Add(Connection:=URL, Destination:=ActiveSheet.Range("a1"))
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "5"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
If Application.CountA(.ResultRange) = 0 Then '非營業日,沒有資料.
xDate = xDate - 1 '往後退一日
GoTo ReDate
End If
End With
End Sub
複製代碼
作者:
pupai
時間:
2013-12-9 17:57
本帖最後由 GBKEE 於 2013-12-9 18:12 編輯
回復
4#
GBKEE
感謝
不懂的很多 但學到也很多
謝謝!!
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)