excel - 在 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"))
解决方案
您需要首先解析/评估“超链接”公式。假设您所有的链接都在 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
祝你好运,
切西
推荐阅读
- c++ - 如何将不同无序映射的地址存储到另一个无序映射中,然后通过引用来使用?
- visual-c++ - MBCS 文件菜单生成 ???? 人物
- javascript - Symfony 3.4 - Webpack Encore 和 DOMPurify
- xml - XSL:根据属性计数和匹配创建组
- windows - 在 Sublime Text 3 中构建时权限被拒绝
- c++ - 在 C++ 中的 if 块内声明变量
- python - Python - time.time() 和 datetime.datetime.utcnow().timestamp() 返回不同的纪元
- c# - 如何使用 RegEx.Replace 替换 . 和 - 到以下日期字符串 01.04-2016 上的 /
- mysql - 无法处理具有特殊字符的代码
- css - 如何使用具有可变大小图像且每行末尾没有白色间隙的包装制作网格