首页 > 解决方案 > 在 Excel VBA 问题中打开超链接

问题描述

我一直在尝试查找/编写一个宏,该宏可以一次打开选定范围内包含的所有超链接。我遇到的代码仅适用于某些类型的超链接,特别是通过右键单击/插入>链接/Ctrl+K 添加的超链接。该代码不会识别使用 HYPERLINK() 函数形成的任何超链接。

这是我在网上找到的代码:

Sub OpenMultipleLinks()
    On Error Resume Next
    Set myRange = Application.Selection
    Set myRange = Application.InputBox("Range", "OpenMultipleLinks", myRange.Address, Type:=8)
    For Each oneLink In myRange.Hyperlinks
        oneLink.Follow
    Next
End Sub

这是成为超链接的单元格的公式。

=IF($D2="All Charts","",HYPERLINK("http://SubstituteWebsite/ChartId="&$D2&$AF$1,"link"))

标签: excelvbahyperlink

解决方案


您需要首先解析/评估“超链接”公式。假设您所有的链接都在 col A 中,这将满足您的要求:

    Sub link()
        Dim arr, arr2, j As Long
        arr = Sheet1.Range("A1").CurrentRegion.Formula2 'get all in an array
        For j = 1 To UBound(arr)
            If Left(arr(j, 1), 3) = "=HY" Then 'check if it's a formula
                arr(j, 1) = Evaluate(Split(Mid(arr(j, 1), 2), ",")(0) & ")") 'split the url from the rest, evaluate and replace in array
            End If
            ActiveWorkbook.FollowHyperlink Address:=arr(j, 1), NewWindow:=True 'open in default browser
        Next j
    End Sub

祝你好运,

切西


推荐阅读