从Excel超链接公式中提取URL

[英]Extract URL From Excel Hyperlink Formula


I have an Excel file with hundreds of cells that use the Hyperlink formula =HYPERLINK( <targetURL>, <friendlyName> ). I need to extract the plain text URLs from these. Most examples that I've found rely on the cell using a different hyperlinking method.

我有一个包含数百个单元格的Excel文件,这些单元格使用超链接公式= HYPERLINK( )。我需要从这些中提取纯文本URL。我发现的大多数示例都依赖于使用不同超链接方法的单元格。

So a function like this:

所以像这样的函数:

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

results in cell text "not found". Alternatively, is there a way of converting these cells to the other hyperlink format so that the macro I have works?

导致单元格文本“未找到”。或者,有没有办法将这些单元格转换为另一种超链接格式,以便我可以使用宏?

6 个解决方案

#1


3  

Here is a method that will return the hyperlink text whether it has been created by a formula, or by the Insert/Hyperlink method.

这是一个方法,它将返回超链接文本,无论它是由公式创建还是由插入/超链接方法创建。

If the former, we merely have to parse the formula; if the latter, we need to iterate through the hyperlinks collection on the worksheet.

如果是前者,我们只需要解析公式;如果是后者,我们需要遍历工作表上的超链接集合。

The formula will return nothing if there is no hyperlink in cell_ref; change to suit.

如果cell_ref中没有超链接,公式将不返回任何内容;改变适合。


Option Explicit
Function HyperLinkText(rg As Range)
    Dim sFormula As String, S As String
    Dim L As Long
    Dim H As Hyperlink, HS As Hyperlinks

sFormula = rg.Formula
L = InStr(1, sFormula, "HYPERLINK(""", vbBinaryCompare)

If L > 0 Then
    S = Mid(sFormula, L + 11)
    S = Left(S, InStr(S, """") - 1)
Else
    Set HS = rg.Worksheet.Hyperlinks
    For Each H In HS
        If H.Range = rg Then
            S = H.Address
        End If
    Next H
End If

HyperLinkText = S

End Function

#2


1  

You could extract it with a regex:

你可以用正则表达式提取它:

Dim re
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "^=HYPERLINK\(""([^""]+)"""

If re.Test(pRange.Formula) Then
    Debug.Print "URL = " & re.Execute(pRange.Formula)(0).SubMatches(0)
Else
    Debug.Print "URL not found"
End If

This just checks to see if the formula begins with:

这只是检查公式是否以下列开头:

=HYPERLINK("

and, if so, grabs the text from that point until the following ".

并且,如果是这样,从该点抓取文本直到以下“。

#3


0  

Hm - playing around with it, I couldn't get .Address to work either.

嗯 - 玩弄它,我无法得到。地址也可以工作。

You say you want to extract the URL only, I was able to do that with this macro:

你说你只想提取URL,我能用这个宏做到这一点:

Function hyperlinkText(pRange As Range) As String
Dim st1 As String, st2 As String
Dim tempSub1 As String, tempSub2 As String

If Left(pRange.Formula, 10) <> "=HYPERLINK" Then
    hyperlinkText = "not found"
    Exit Function
Else
    tempSub1 = WorksheetFunction.Substitute(pRange.Formula, """", "[", 1)
    tempSub2 = WorksheetFunction.Substitute(tempSub1, """", "]", 1)
    hyperlinkText = Mid(tempSub2, WorksheetFunction.Find("[", tempSub2) + 1, WorksheetFunction.Find("]", tempSub2) - WorksheetFunction.Find("[", tempSub2) - 1)
End If

End Function

Note though, it doesn't get the "Friendly Name" of the Hyperlink() formula, just the URL.

但请注意,它不会获取Hyperlink()公式的“友好名称”,只会获取URL。

#4


0  

Non-VBA possibility:

非VBA可能性:

Work on copies of the cells with links because the first step is to replace part of their content (specifically = with I suggest the not sign ¬). Then, assuming the copy is in A1:

使用链接处理单元格的副本,因为第一步是替换部分内容(特别是=我建议不签名¬)。然后,假设副本在A1中:

=SUBSTITUTE(LEFT(MID(A1,13,LEN(A1)),FIND("""",MID(A1,13,LEN(A1)))-1),"¬","=")  

and replace ¬ with = where the link contains an equals sign.

并将¬替换为=其中链接包含等号。

#5


0  

I ended up using Python:

我最终使用Python:

  1. Download (or convert) the spreadsheet into xlsx format.
  2. 将电子表格下载(或转换)为xlsx格式。
  3. Install openpyxl using pip or conda.
  4. 使用pip或conda安装openpyxl。
  5. Read the xlsx with a code similar to:

    使用类似于的代码读取xlsx:

    from openpyxl import load_workbook
    wb = load_workbook(filename = 'cities.xlsx')
    print(wb.worksheets)
    print(dir(wb))
    sheet_ranges = wb['Sheet1']
    for c in sheet_ranges['B']:
        print(c.hyperlink.target)
    

Note that the name 'Sheet1' or column names might be different case by case ('B' is the column with hyperlink in my case).

请注意,名称“Sheet1”或列名称可能因具体情况而异(“B”是我的情况下带有超链接的列)。

  1. After the links are printed copy them and paste them into a new column in your sheet.
  2. 打印链接后,将它们复制并粘贴到工作表中的新列中。

#6


0  

Here is an Excel formula that can extract URL from a hyperlink used into cell.

这是一个Excel公式,可以从用于单元格的超链接中提取URL。

A1= Excel Cell where you want to extract URL.

A1 =要提取URL的Excel单元格。

=MID(FORMULATEXT(A1),FIND(CHAR(34),FORMULATEXT(A1))+1,FIND(CHAR(34),FORMULATEXT(A1),FIND(CHAR(34),FORMULATEXT(A1))+1)-1-FIND(CHAR(34),FORMULATEXT(A1)))

Excel Formula to get URL from excel hyperlink [Image]

Excel公式从excel超链接获取URL [图片]


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2015/08/26/5634c643ec8f99235fd6ebd61d4a8477.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号