Extract URL from =Hyperlink()

2019-09-06 02:51发布

I'm trying to extract the formula that is evaluated from a link generated with a formula:

=HYPERLINK("https://www.google.com/search?q="&A1&B1,"Link")
(A1 has vba and B1 has help)

I've seen many, many threads and even some SO threads with suggestions, but they only get me to the ...q= without considering I have more text to come.

The best luck I've had so far is from this thread, and I've tweaked it to search until B1 like this:

...
S = Left(S, InStr(S, "B17") + 2)
...

But it returns https://www.google.com/search?q="&A1&B1.

How can I get it to first evaluate what's in those cells, before returning the URL?

1条回答
神经病院院长
2楼-- · 2019-09-06 03:15

I was overthinking this I think. Thanks to @MacroMan for getting my head straight. I put together the following, rather clunky, macro.

Function hyperlinkText(rg As Range) As String
' Inspired by https://stackoverflow.com/questions/32230657/extract-url-from-excel-hyperlink-formula/32233083#32233083
Dim sFormula As String
Dim Test As String

sFormula = rg.Formula
Test = Mid(sFormula, WorksheetFunction.Search("""", sFormula), WorksheetFunction.Search(",", sFormula) - WorksheetFunction.Search("""", sFormula))
hyperlinkText = Evaluate("=" & Test)
End Function

This can take a URL that looks like:
=HYPERLINK("https://www.google.com/search?q="&A17&B17,"Link") and return the evaluated URL:

enter image description here

查看更多
登录 后发表回答