返回列表 上一主題 發帖

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

Sub qq()
Set shFirstQtr = ActiveSheet
Set qtQtrResults = shFirstQtr.QueryTables _
    .Add(Connection:="URL;http://tw.stock.yahoo.com/d/s/company_" & [a1] & ".html", _
        Destination:=shFirstQtr.Cells(3, 1))
With qtQtrResults
    .WebFormatting = xlNone
    .WebSelectionType = xlSpecifiedTables
    .WebTables = "9"
    .Refresh
End With
End Sub
我想問問那麼在這段代碼中
.Refreshstyle 可以插在那個位置??
因為我想這個是覆蓋資料而不是不斷插入

TOP

謝謝oobird版主大大。
感謝您引領小弟進入
另一個新的領域。

感恩大大!

TOP

Dim qtQtrResults As QueryTable

TOP

謝謝oobird版主大大。
感謝您幫小弟開啟這一扇大門,
同時也幫小弟搬開心中的一塊
大石頭。
一語點醒如此好用的EXCEL
錄製功能,小弟卻將它遺忘。

另小弟想宣告設定
DIM qtQtrResults AS ......
才能讓它在WITH qtQtrResults時
能顯示出其屬性及功能呢?
With qtQtrResults
    .WebFormatting = xlNone
    .WebSelectionType = xlSpecifiedTables
    .WebTables = "9"
    .Refresh
End With

感恩大大!

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

其實你錄製一下就可得到代碼,再把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取得網頁資料,
一直深深感到興趣但是無法領會及突破困境。
   現想借用此板及板主大大提供的語法。
並取得此網頁的指定位置內容如下:

一、
   網址:http://tw.stock.yahoo.com/d/s/company_1102.html

二、
   取得此網頁的資料表為

獲 利 能 力 (100第2季) 最新四季每股盈餘  最近四年每股盈餘

三、
  小弟有試著直接將下列語法的網址更正
如上的網址但是無法成功。
能請版主大大指點小弟。

感恩大大:


Sub 新增查詢()
    Range("A1") = "2610"   '股票代號
    Range("A3").Select
    With ActiveSheet.QueryTables.Add(Connection:= _
        "URL;http://tw.stock.yahoo.com/q/q?s=" & Range("a1"), Destination:=Selection)  '新增查詢
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebTables = "6"
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
        .Name = .ResultRange.Cells(3, 1)
    End With
   End Sub
'***** 已新增後查詢後 可修改A1的股票代號 查詢  *****
Sub 更新查詢()
    With Range("A3").QueryTable
        .Connection = "URL;http://tw.stock.yahoo.com/q/q?s=" & Range("a1") '股票代號在[A1]
        .Refresh BackgroundQuery:=False
        .Name = .ResultRange.Cells(3, 1)
    End With
End Sub

TOP

感謝發文.真的可以用..是否還有同時能抓多檔的方法?

TOP

感謝版主指導~
不進則退

TOP

回復 6# mnsmrtl
另外想再請教,如何只回傳其中一個欄位?
請參考 http://forum.twbts.com/thread-3064-1-2.html

webtables="6",要怎麼取得?




TOP

        靜思自在 : 有願放在心裡,沒有身體力行,正如耕田不播種,皆是空過因緣。
返回列表 上一主題