使用 VBA 从 Excel 中提取 URL,而不是在散列后提取

Extract URL from Excel using VBA not extracting after hash

我正在使用 VBA 方法从单元格中提取 URL:

Sub ExtractHL()
Dim HL As Hyperlink
For Each HL In ActiveSheet.Hyperlinks
HL.Range.Offset(0, 1).Value = HL.Address
Next
End Sub

这是在 # 示例之前提取 URL 中的所有内容: 如果整个 URL https://www.w3.org/TR/html4/sgml/entities.html#h-24.4.1 my results will only show: https://www.w3.org/TR/html4/sgml/entities.html 缺少 #h-24.4.1 有没有人有 VBA 解决方案来提取整个 URL 包括哈希和之后的所有内容?

我通常创建一个函数。在工作簿模块中粘贴以下函数:

Function HLink(rng As Range) As String
'extract URL from hyperlink
  If rng(1).Hyperlinks.Count Then HLink = rng.Hyperlinks(1).Address
End Function

现在将函数用作公式,如下所示:

  1. 在 A1 中你有 URL
  2. 在 A2 中你有新公式 =hlink(A1)

PS:来自 Rick Rothstein

的原创

我在另一个 site 上找到了答案和解释。 #之后的link部分被视为子地址,通过包含.SubAddress问题已经解决。

Function GetURL(cell As range, _
Optional default_value As Variant)
'Lists the Hyperlink Address for a Given Cell
'If cell does not contain a hyperlink, return default_value
If (cell.range("A1").Hyperlinks.Count <> 1) Then
GetURL = default_value
Else
GetURL = cell.range("A1").Hyperlinks(1).Address & "#" &
cell.range("A1").Hyperlinks(1).SubAddress
End If
End Function

当我可以预测 pound/hash 符号的放置位置时,这对我有用。这是我在网上找到的另一个“GetURL”函数的迭代。

Function GetURLs(HyperlinkCell As Range)

    GetURLs = HyperlinkCell.Hyperlinks(1).Address + "#" + HyperlinkCell.Hyperlinks(1).SubAddress

End Function