【问题标题】:Get the specific URL from the website link name (using VBA)从网站链接名称中获取特定的 URL(使用 VBA)
【发布时间】:2016-09-13 03:35:05
【问题描述】:

您好,我在这里也找到了下面的代码……但这会导致所有链接都复制到工作表中……如果我只想从特定链接名称复制 URL,该怎么办?请帮我编码。例如下面的代码...我想要这个链接名称中的 URL“Excel VBA 入门 - 我的程序员”。应该导致单元格 A1 中的 URL“http://www.i-programmer.info/ebooks/automating-excel/1264-getting-started.html” 另见图片...提前致谢!!!

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

【问题讨论】:

    标签: excel vba url hyperlink


    【解决方案1】:

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

    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
    

    【讨论】:

    • 哦,它的工作......但是当我申请我的真实项目时它不起作用。我将 URL 更改为 URL = "http://iweb.mywork.com/location/place/en-US/OnD/DR/DR%20Form/Pages/default.aspx" Got runt-time-error 自动化错误,调用的对象已与其客户端断开连接...当我单击调试时,它以黄色突出显示此行 --> Do Until internet.readyState >= 4...任何帮助请...谢谢
    • 我找到了超时错误的解决方案...Insted of Do loop..我使用了while wend ....但我有下一个问题...在调试模式下,箭头指向这一行 --> 设置 internetdata = internet.Document...请帮助
    猜你喜欢
    • 2016-09-10
    • 2018-06-26
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    相关资源
    最近更新 更多