使用vbs将网页的特定单词复制到excel

时间:2013-11-07 20:23:06

标签: excel vba excel-vba vbscript

我想将网页中存在的特定文本“110001”复制到Excel文件中的工作表中。请告诉我如何通过vbscript做到这一点。下面是我要复制的相关源代码行。

<h1 id="h1">110001</h1>

以下是我正在使用的代码。此代码中的问题是它没有从网页复制任何内容。此外,它在第3行最后一行给出运行时错误1004 - 范围(“A1”)。选择

Private Sub blahblah_Click()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://someWebsite"
    Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
    Loop
Set y = ie.Document.getelementbyid("h1")
ie.ExecWB 12, 2
Workbooks("Book1.xlsm").Activate
Sheets("Sheet2").Select
Range("A1").Select
ActiveSheet.Paste
End Sub

3 个答案:

答案 0 :(得分:1)

y = ie.document.getElementById(“ad-space.holdings”)。innerHTML

以上代码显示错误或err.number = 424 ..

让我知道我是否需要从VBA中的引用中选择任何内容?

答案 1 :(得分:0)

可以避免复制粘贴吗?试试这个。

Private Sub blahblah_Click()
Dim ie As Object
Dim WB As Workbook
Dim WS As Worksheet
Set WB = Workbooks("Book1.xlsm")
Set WS = WB.Sheets("Sheet2")
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "http://someWebsite"
    Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
    Loop
y = ie.Document.getelementbyid("h1").innerHTML
WS.Range("A1").Value = y
End Sub

基本上,您需要innerHTML部分,因为这是获取具有该元素ID的HTML标记内的值所需的控件。

答案 2 :(得分:0)

Private Sub blahblah_Click()
Dim ie As Object
Dim WB As Workbook
Dim WS As Worksheet
Set WB = Workbooks.Add
Set WS = WB.Sheets("Sheet2")
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "http://etfdb.com/etf/EEM/#holdings"
    Do While ie.Busy
Application.Wait DateAdd("s", 1, Now)
    Loop
y = ie.document.getElementById("ad-space.holdings").innerHTML
WS.Range("A1").Value = y
End Sub