[英]Extract URL From Excel Hyperlink Formula
我有一个 Excel 文件,其中包含数百个使用超链接公式=HYPERLINK( <targetURL>, <friendlyName> )
的单元格。 我需要从中提取纯文本 URL。 我发现的大多数示例都依赖于使用不同超链接方法的单元格。
所以像这样的 function :
Function HyperLinkText(pRange As Range) As String
Dim ST1 As String
Dim ST2 As String
If pRange.Hyperlinks.Count = 0 Then
HyperLinkText = "not found"
Exit Function
End If
ST1 = pRange.Hyperlinks(1).Address
ST2 = pRange.Hyperlinks(1).SubAddress
If ST2 <> "" Then
ST1 = "[" & ST1 & "]" & ST2
End If
HyperLinkText = ST1
End Function
导致单元格文本“未找到”。 或者,有没有办法将这些单元格转换为其他超链接格式,以便我拥有的宏可以工作?
我有一个包含数百个使用超链接公式=HYPERLINK( <targetURL>, <friendlyName> )
的单元格的Excel文件。 我需要从中提取纯文本URL。 我发现的大多数示例都使用不同的超链接方法来依赖该单元格。
所以这样的功能:
Function HyperLinkText(pRange As Range) As String
Dim ST1 As String
Dim ST2 As String
If pRange.Hyperlinks.Count = 0 Then
HyperLinkText = "not found"
Exit Function
End If
ST1 = pRange.Hyperlinks(1).Address
ST2 = pRange.Hyperlinks(1).SubAddress
If ST2 <> "" Then
ST1 = "[" & ST1 & "]" & ST2
End If
HyperLinkText = ST1
End Function
导致单元格文本“未找到”。 另外,是否有一种方法可以将这些单元格转换为其他超链接格式,以使我的宏正常工作?
我有一个包含数百个使用超链接公式=HYPERLINK( <targetURL>, <friendlyName> )
的单元格的Excel文件。 我需要从中提取纯文本URL。 我发现的大多数示例都使用不同的超链接方法来依赖该单元格。
所以这样的功能:
Function HyperLinkText(pRange As Range) As String
Dim ST1 As String
Dim ST2 As String
If pRange.Hyperlinks.Count = 0 Then
HyperLinkText = "not found"
Exit Function
End If
ST1 = pRange.Hyperlinks(1).Address
ST2 = pRange.Hyperlinks(1).SubAddress
If ST2 <> "" Then
ST1 = "[" & ST1 & "]" & ST2
End If
HyperLinkText = ST1
End Function
导致单元格文本“未找到”。 另外,是否有一种方法可以将这些单元格转换为其他超链接格式,以使我的宏正常工作?
我有一个包含数百个使用超链接公式=HYPERLINK( <targetURL>, <friendlyName> )
的单元格的Excel文件。 我需要从中提取纯文本URL。 我发现的大多数示例都使用不同的超链接方法来依赖该单元格。
所以这样的功能:
Function HyperLinkText(pRange As Range) As String
Dim ST1 As String
Dim ST2 As String
If pRange.Hyperlinks.Count = 0 Then
HyperLinkText = "not found"
Exit Function
End If
ST1 = pRange.Hyperlinks(1).Address
ST2 = pRange.Hyperlinks(1).SubAddress
If ST2 <> "" Then
ST1 = "[" & ST1 & "]" & ST2
End If
HyperLinkText = ST1
End Function
导致单元格文本“未找到”。 另外,是否有一种方法可以将这些单元格转换为其他超链接格式,以使我的宏正常工作?
我有一个包含数百个使用超链接公式=HYPERLINK( <targetURL>, <friendlyName> )
的单元格的Excel文件。 我需要从中提取纯文本URL。 我发现的大多数示例都使用不同的超链接方法来依赖该单元格。
所以这样的功能:
Function HyperLinkText(pRange As Range) As String
Dim ST1 As String
Dim ST2 As String
If pRange.Hyperlinks.Count = 0 Then
HyperLinkText = "not found"
Exit Function
End If
ST1 = pRange.Hyperlinks(1).Address
ST2 = pRange.Hyperlinks(1).SubAddress
If ST2 <> "" Then
ST1 = "[" & ST1 & "]" & ST2
End If
HyperLinkText = ST1
End Function
导致单元格文本“未找到”。 另外,是否有一种方法可以将这些单元格转换为其他超链接格式,以使我的宏正常工作?
我有一个包含数百个使用超链接公式=HYPERLINK( <targetURL>, <friendlyName> )
的单元格的Excel文件。 我需要从中提取纯文本URL。 我发现的大多数示例都使用不同的超链接方法来依赖该单元格。
所以这样的功能:
Function HyperLinkText(pRange As Range) As String
Dim ST1 As String
Dim ST2 As String
If pRange.Hyperlinks.Count = 0 Then
HyperLinkText = "not found"
Exit Function
End If
ST1 = pRange.Hyperlinks(1).Address
ST2 = pRange.Hyperlinks(1).SubAddress
If ST2 <> "" Then
ST1 = "[" & ST1 & "]" & ST2
End If
HyperLinkText = ST1
End Function
导致单元格文本“未找到”。 另外,是否有一种方法可以将这些单元格转换为其他超链接格式,以使我的宏正常工作?
我有一个包含数百个使用超链接公式=HYPERLINK( <targetURL>, <friendlyName> )
的单元格的Excel文件。 我需要从中提取纯文本URL。 我发现的大多数示例都使用不同的超链接方法来依赖该单元格。
所以这样的功能:
Function HyperLinkText(pRange As Range) As String
Dim ST1 As String
Dim ST2 As String
If pRange.Hyperlinks.Count = 0 Then
HyperLinkText = "not found"
Exit Function
End If
ST1 = pRange.Hyperlinks(1).Address
ST2 = pRange.Hyperlinks(1).SubAddress
If ST2 <> "" Then
ST1 = "[" & ST1 & "]" & ST2
End If
HyperLinkText = ST1
End Function
导致单元格文本“未找到”。 另外,是否有一种方法可以将这些单元格转换为其他超链接格式,以使我的宏正常工作?
其他答案不能很好地处理公式中的变化。 例如,如果公式同时包含 LINK_LOCATION 参数和 FRIENDLY_NAME 参数,它们就会失败。 如果公式在某些区域有额外的空格或换行符,其他的也会失败。
这个答案并不完美,但它比我在发布之日找到的其他答案更好。 我已经确定了此代码可以工作以及失败的情况。
这个 VBA function 有点长,但它会从 HYPERLINK() 公式或嵌入在单元格中的非公式超链接中提取超链接的 URL/地址。
它首先检查非公式超链接,因为这是最容易和最可靠地提取的超链接。 如果不存在,它会检查公式中的超链接。
仅当 HYPERLINK() function 除了等号之外没有任何内容时,从公式中提取才有效。
它将适用于这个公式:
=HYPERLINK("https://" & A1, "My Company Website")
它也适用于这个公式(注意额外的空格和换行符):
=
HYPERLINK( "https://" & A1,
"My Company Website" & B2)
它不适用于此公式:
=IF( LEN(A1)=0, "", HYPERLINK("https://" & A1, "My Company Website") )
Function HyperLinkText(ByVal Target As Excel.Range) As String
' If TARGET is multiple cells, only check the first cell.
Dim firstCellInTarget As Excel.Range
Set firstCellInTarget = Target.Cells.Item(1)
Dim returnString As String
' First check if the cell contains a non-formula hyperlink.
If Target.Hyperlinks.Count > 0 Then
' Cell contains a non-formula hyperlink.
returnString = Target.Hyperlinks.Item(1).Address ' extract hyperlink text from the Hyperlinks property of the range
Else
' Cell does -NOT- contain a non-formula hyperlink.
' Check for a formula hyperlink.
Dim targetFormula As String
targetFormula = firstCellInTarget.Formula
Dim firstOpenParenthesisIndex As Long
firstOpenParenthesisIndex = VBA.InStr(1, _
targetFormula, _
"(", _
VbCompareMethod.vbBinaryCompare)
Dim cleanFormulaHyperlinkPrefix As String
cleanFormulaHyperlinkPrefix = Left$(targetFormula, firstOpenParenthesisIndex)
cleanFormulaHyperlinkPrefix = Replace$(Replace$(Replace$(cleanFormulaHyperlinkPrefix, Space$(1), vbNullString), vbCr, vbNewLine), vbLf, vbNullString)
Dim cleanFormulaPart2 As String
cleanFormulaPart2 = Mid$(targetFormula, firstOpenParenthesisIndex + 1)
Dim cleanFormulaCombined As String
cleanFormulaCombined = cleanFormulaHyperlinkPrefix & cleanFormulaPart2
' Get all text inside the HYPERLINK() function.
' This is either a single LINK_LOCATION parameter or both the
' LINK_LOCATION and FRIENDLY_NAME parameters separated by a comma.
'
' Ex. 1 Parameter: "https://" & $A$1
' Ex. 2 Parameters: "https://" & $A$1, "Click Here To Open the Company URL"
'
Const HYPERLINK_FORMULA_PREFIX As String = "=HYPERLINK("
Dim tmpString As String
tmpString = Mid$(cleanFormulaCombined, VBA.Len(HYPERLINK_FORMULA_PREFIX) + 1)
Dim textInsideHyperlinkFunction As String
textInsideHyperlinkFunction = Left$(tmpString, VBA.Len(tmpString) - 1)
' Get the first parameter (LINK_LOCATION) from the text inside the HYPERLINK()
' function by using =EVALUATE(). If text inside the HYPERLINK() function
' contains two parameters, they will be separated by a comma and EVALUATE()
' will return an error. Start with the entire text inside the HYPERLINK()
' function. If EVALUATE() returns an error, remove one character from the end
' of the string being evaluated and try again. Eventually only one parameter
' will be evaluated and EVALUATE() will return a text string.
'
' For example, if the string to be evaluated is:
'
' "https://" & $A$1, "Click Here To Open the Company URL"
'
' and cell A1 contains:
'
' mycompany.com
'
' EVALUATE will return:
'
' https://mycompany.com
'
Dim hyperlinkLinkLocation As String
Dim i As Long
For i = VBA.Len(textInsideHyperlinkFunction) To 1 Step -1 ' with each failure, shrink length of string-to-evaluate by one
If Not VBA.IsError(Excel.Application.Evaluate("=" & Left$(textInsideHyperlinkFunction, i))) Then
hyperlinkLinkLocation = Excel.Application.Evaluate("=" & Left$(textInsideHyperlinkFunction, i))
Exit For ' ****
End If
Next i
returnString = hyperlinkLinkLocation
End If
' Return the hyperlink string.
HyperLinkText = returnString
End Function
Sub Test()
' Display hyperlink of the first cell
' in the currently selected range.
Msgbox HyperLinkText(Selection) ' displays the hyperlink of the first cell
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.