简体   繁体   English

使用VBA在Excel中打开多个链接

[英]Open multiple links in Excel with VBA

I have the following code that should open multiple links in excel all at once 我有以下代码应一次在excel中打开多个链接

Sub OpenLink()
    Dim xHyperlink As Hyperlink
    Dim WorkRng As Range
    On Error Resume Next
    xTitleId = "Select Area"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
    For Each xHyperlink In WorkRng.Hyperlinks
        xHyperlink.Follow
    Next
End Sub

Which, however, it is not working and I cannot understand why. 但是,这不起作用,我不明白为什么。

My links are of the form =HYPERLINK("http://example.com"; "Wording") and they work standalone. 我的链接的格式为=HYPERLINK("http://example.com"; "Wording") ,它们可以独立工作。

I believe this is not work as you are using the hyperlink function (instead of right click > hyperlink..) 我认为这不起作用,因为您正在使用超链接功能(而不是右键单击>超链接..)

You can prove that this is the issue by right click assigning a hyperlink to a cell and testing your code there. 通过右键单击为单元格分配超链接并在其中测试代码,可以证明这是问题所在。

Are you getting the link address from another cell? 您是从另一个单元获取链接地址吗? if so you could go straight there and open the link using ActiveWorkbook.FollowHyperlink or you could use a technique like this reading the hyperlink value from a formula: 如果是这样,您可以直接去那里并使用ActiveWorkbook.FollowHyperlink打开链接,也可以使用类似的方法从公式中读取超链接值:

https://stackoverflow.com/a/42564907/359135 https://stackoverflow.com/a/42564907/359135

Some working (hopefully) code that extracts the url from the formula and opens it: 一些有效的代码(希望)从公式中提取网址并打开它:

Sub OpenLinks()
    Dim xHyperlink As Hyperlink
    Dim WorkRng As Range
    On Error Resume Next
    xTitleId = "Select Area"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
    For Each c In WorkRng.Cells
      Dim sFormula As String
      Dim l As String
      sFormula = c.Formula
      l = Mid(sFormula, WorksheetFunction.Search("""", sFormula) + 1, WorksheetFunction.Search(",", sFormula) - WorksheetFunction.Search("""", sFormula) - 2)
      ActiveWorkbook.FollowHyperlink l
    Next
End Sub

I had to tweak the code from that example to not include the quotation marks when extracting the url from the formula text. 从公式文本中提取URL时,我必须调整该示例中的代码以不包括引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM