返回列表 上一主題 發帖

[已解決]如何抓取網頁資料

壓縮成rar或zip格式才可上傳。

TOP

依你現有代碼,加上循環就行了。
此例取得今年的資料:
  1. Sub Macro1()
  2.     d = DateValue("2011/10/17")
  3.     i = 3
  4.     Do
  5.         dt = Application.Text(d, "yyyy/mm/dd")
  6.         Set s = Sheet2
  7.         s.UsedRange.Clear
  8.         With s.QueryTables.Add(Connection:= _
  9.                                "URL;http://www.trademag.org.tw/ExchangeRate.asp?vdate=" & dt, _
  10.                                Destination:=s.Range("$A$1"))
  11.             .Name = "17"
  12.             .FieldNames = True
  13.             .RowNumbers = False
  14.             .FillAdjacentFormulas = False
  15.             .PreserveFormatting = True
  16.             .RefreshOnFileOpen = False
  17.             .BackgroundQuery = True
  18.             .RefreshStyle = xlInsertDeleteCells
  19.             .SavePassword = False
  20.             .SaveData = True
  21.             .AdjustColumnWidth = True
  22.             .RefreshPeriod = 0
  23.             .WebSelectionType = xlSpecifiedTables
  24.             .WebFormatting = xlWebFormattingNone
  25.             .WebTables = "2"
  26.             .WebPreFormattedTextToColumns = True
  27.             .WebConsecutiveDelimitersAsOne = True
  28.             .WebSingleBlockTextImport = False
  29.             .WebDisableDateRecognition = False
  30.             .WebDisableRedirections = False
  31.             .Refresh BackgroundQuery:=False
  32.         End With
  33.         With Sheet1
  34.             For j = s.[iv3].End(1).Column To 3 Step -1
  35.                 a = s.Cells(3, j).Resize(41, 1).Value
  36.                 .Cells(i, 2) = s.Cells(1, j)
  37.                 .Cells(i, 3).Resize(1, 41) = Application.Transpose(a)
  38.                 i = i + 1
  39.             Next
  40.         End With
  41.         d = d - 7
  42.     Loop Until Year(d) < 2011
  43. End Sub
複製代碼

TOP

這只是應你的要求修改你的程式
達到取歷史記錄的目的
其實該程式只要跑一遍,以後就用不到了
以後的只要每個星期更新一次,把目前範往下移,在前幾行寫入新的數據
這又是另一個不同的程式。
不必每次更新都要全程跑一遍的!

TOP

網頁你沒看嗎?星期6、日本就沒有資料的。

TOP

s.UsedRange.Clear  
s是sheet2
清空sheet2的使用範圍,以便寫入新的一筆資料呀。   
   [iv3]是甚麼?

就是儲存格iv3  
s.[iv3].End(1).Column To 3 Step -1  
表示sheet2從iv3往左的第一個有資料的欄號到3的反向循環

TOP

        靜思自在 : 不要隨心所欲,要隨心教育自己。
返回列表 上一主題