2016-05-17 229 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