2016-05-17 182 views
-1

你好,我在这里也找到了下面的代码......但是这会导致所有链接复制到工作表......如果我只想从特定网址复制URL链接名称?请帮我编码。例如对于下面的代码...我想从这个链接名称的URL“Excel VBA - I程序员入门”。应该在单元格A1中产生一个URL“http://www.i-programmer.info/ebooks/automating-excel/1264-getting-started.html” 查看图片也...预先感谢!!!从网站链接名称(使用VBA)获取特定网址

Sub webpage() 

    Dim internet As Object 
    Dim internetdata As Object 
    Dim div_result As Object 
    Dim header_links As Object 
    Dim link As Object 
    Dim URL As String 

    Set internet = CreateObject("InternetExplorer.Application") 
    internet.Visible = True 

    URL = "https://www.google.co.in/search?q=how+to+program+in+vba" 
    internet.Navigate URL 

    Do Until internet.ReadyState >= 4 
     DoEvents 
    Loop 

    Application.Wait Now + TimeSerial(0, 0, 5) 

    Set internetdata = internet.Document 
    Set div_result = internetdata.getelementbyid("res") 


    Set header_links = div_result.getelementsbytagname("h3") 

    For Each h In header_links 
     Set link = h.ChildNodes.Item(0) 
     Cells(Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = link.href 
    Next 

    MsgBox "done" 
End Sub 

enter image description here

回答

0

您可以检查innerText属性的值添加URL到工作表

For Each h In header_links 
    If h.innerText = "Getting started with Excel VBA - I Programmer" Then 
     Set link = h.ChildNodes.Item(0) 
     Cells(Range("A" & Rows.Count).End(xlUp).Row + 1, 1) = link.href 
    End If 
Next 
+0

哦,它的工作之前......但是当我申请我真的项目没有关系”工作。我将网址更改为了“URL =”http://iweb.mywork.com/location/place/en-US/OnD/DR/DR%20Form/Pages/default.aspx“'得到了runt-time-error自动化错误,被调用的对象已经从它的客户端断开...当我点击调试它突出显示在黄色这一行 - >'做直到internet.readyState> = 4' ...任何帮助请...谢谢 – 200yrs

+0

我发现解决方案对于超时错误... ins的Do循环..我使用时,我们......但我有下一个问题......在调试模式下,指向这一行的箭头 - > Set internetdata = internet.Document ...请帮助 – 200yrs