[英]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.