返回列表 上一主題 發帖

[發問] 請問如何以VBA擷取網站上的資料?

其實你錄製一下就可得到代碼,再把2610改成儲存格位址如[a1], 如此而已。
  1. Sub Macro2()
  2.     With ActiveSheet.QueryTables.Add(Connection:= _
  3.         "URL;http://tw.stock.yahoo.com/d/s/company_" & [a1] & ".html", _
  4.         Destination:=Range("A3"))
  5.         .Name = "company_" & [a1]
  6.         .FieldNames = True
  7.         .RowNumbers = False
  8.         .FillAdjacentFormulas = False
  9.         .PreserveFormatting = True
  10.         .RefreshOnFileOpen = False
  11.         .BackgroundQuery = True
  12.         .RefreshStyle = xlInsertDeleteCells
  13.         .SavePassword = False
  14.         .SaveData = True
  15.         .AdjustColumnWidth = True
  16.         .RefreshPeriod = 0
  17.         .WebSelectionType = xlSpecifiedTables
  18.         .WebFormatting = xlWebFormattingNone
  19.         .WebTables = "9"
  20.         .WebPreFormattedTextToColumns = True
  21.         .WebConsecutiveDelimitersAsOne = True
  22.         .WebSingleBlockTextImport = False
  23.         .WebDisableDateRecognition = False
  24.         .WebDisableRedirections = False
  25.         .Refresh BackgroundQuery:=False
  26.     End With
  27. End Sub
複製代碼

TOP

這是excel的說明中的範例寫法,比錄製得的代碼簡潔多了
  1. Sub qq()
  2. Set shFirstQtr = ActiveSheet
  3. Set qtQtrResults = shFirstQtr.QueryTables _
  4.     .Add(Connection:="URL;http://tw.stock.yahoo.com/d/s/company_" & [a1] & ".html", _
  5.         Destination:=shFirstQtr.Cells(3, 1))
  6. With qtQtrResults
  7.     .WebFormatting = xlNone
  8.     .WebSelectionType = xlSpecifiedTables
  9.     .WebTables = "9"
  10.     .Refresh
  11. End With
  12. End Sub
複製代碼

TOP

Dim qtQtrResults As QueryTable

TOP

        靜思自在 : 不怕事多,只怕多事。
返回列表 上一主題