[英]Copy data from Web to excel using VBA
我有一個網頁,需要我的代碼來復制頁面中的整個數據並將其復制到 Excel 表中,但現在沒有發生這種情況。 我的 excel 表將完全空白。 我認為^a
功能不適用於 IE 來選擇數據然后復制它。
任何幫助深表感謝。 下面是我正在使用的代碼。
Sub Webdata()
Dim assetname As String, country As String, area As String, region As String, pth As String, folname As Variant, assetname1 As String
Website = "http://website.com/"
Set myIE = CreateObject("InternetExplorer.Application")
myIE.Navigate source
myIE.Visible = True
Application.Wait Now + TimeSerial(0, 0, 10)
SendKeys "^a"
Application.Wait Now + TimeSerial(0, 0, 2)
SendKeys "^c"
Application.Wait Now + TimeSerial(0, 0, 2)
Sheets.Add
ActiveSheet.Name = "Webdata"
ActiveSheet.Paste
Application.Wait Now + TimeSerial(0, 0, 2)
Range("A1").Select
Cells.Find(What:="Api Number", After:=ActiveCell, LookIn:= _
xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.Offset(1, 0).Select
Selection.Copy
Sheets("Sheet1").Activate
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
myIE.Quit
Set myIE = Nothing
Err.Clear
Sheets("Webdata").Select
ActiveSheet.Delete
End Sub
那個表格是一團糟,所以我不會像通常那樣花時間完善如何將表格寫到工作表上,即在行內循環表格行和表格單元格,我會堅持你復制表格的想法,但使用剪貼板,帶有.SetText
,而不是SendKeys
。 感興趣的表位於嵌套框架內,因此您必須先協商這些框架。
Set hTable = .frames(2).document.getElementsByTagName("table")(0)
代碼:
Option Explicit
Public Sub GetInfo()
Dim IE As New InternetExplorer, html As HTMLDocument, hTable As HTMLTable, clipboard As Object
Application.ScreenUpdating = False
With IE
.Visible = True
.navigate "http://pipeline.wyo.gov/Wellapi.cfm?oops=IDxxxxx&nAPINO=xxxxxx" '<==Input your personal URL here
While .Busy Or .readyState < 4: DoEvents: Wend
Set html = .document
With html
Set hTable = .frames(2).document.getElementsByTagName("table")(0)
Set clipboard = New MSForms.DataObject
clipboard.SetText hTable.outerHTML
clipboard.PutInClipboard
ActiveSheet.Cells(1, 1).PasteSpecial
End With
.Quit
End With
Application.ScreenUpdating = True
End Sub
參考:
VBE> 工具> 參考資料:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.