使用VBA复制URL并粘贴到Excel中

时间:2011-11-01 09:42:14

标签: excel vba

我编写了一些简单的代码来搜索IMDB搜索页面中的电影名称并提交表单。如何捕获当前URL并将其放入Excel。

以下是我用于搜索IMDB的代码。

Sub IMDB_URL_Search()
    Movie = "The Shawshank Redemption"
    Range("IV1").Select
    ActiveSheet.HyperLinks.Add Anchor:=Selection, Address:= _
        "http://www.imdb.com/find?s=all&q=" & Replace(Movie, " ", "+") _
        , TextToDisplay:="Link"
    Range("IV1").Select
    Selection.HyperLinks(1).Follow NewWindow:=False, AddHistory:=True
End Sub

谢谢, 苏雷什

1 个答案:

答案 0 :(得分:0)

Excel有一个内置的'Hyperlink'类来处理这个问题。您只需要创建一个超链接变量来存储来自“添加”功能的返回超链接。从那里,您可以通过“地址”属性引用您指向的URL。

Sub IMDB_URL_Search()

    Dim movie As String
    Dim link As Hyperlink

    movie = "The Shawshank Redemption"

    Range("IV1").Select

    Set link = ActiveSheet.Hyperlinks.Add(Anchor:=Selection, Address:= _
        "http://www.imdb.com/find?s=all&q=" & Replace(movie, " ", "+") _
        , TextToDisplay:="Link")

    link.Follow NewWindow:=False, AddHistory:=True

    'link.address will be the URL for this web link
    MsgBox link.Address
End Sub
相关问题