Excel将超链接公式转换为具有友好名称的链接

时间:2016-04-04 14:53:15

标签: excel hyperlink

我需要将具有HYPERLINK公式的单元格列转换为仅带有链接到link_location的friendly_name的单元格。例如,= HYPERLINK(" https://www.google.com/search?hl=en&q="& SUBSTITUTE(A2,""," +")&" & tbm = isch& gws_rd = ssl"," G图像")到其中只有G图像并且超链接到公式产生的超链接的单元格,例如https://www.google.com/search?hl=en&q=search+term&tbm=isch&gws_rd=ssl。我可以通过粘贴到Word并粘贴回Excel来完成此操作,但我需要在Excel中执行此操作,无论是通过某些复制/粘贴技巧还是宏。

2 个答案:

答案 0 :(得分:1)

在 Chihiro, Excel Ninja 找到这个: 一种方法是复制并粘贴到 Word Doc。 然后从 Word Doc 复制粘贴回 Excel。这将保留链接和标题/友好名称。

对于大型文档或许多带有链接的列,这当然不太实用。

答案 1 :(得分:0)

我想我明白了,试试这个(根据需要调整rng):

编辑:新代码,每条评论如下:

Sub replace_Hyperlink_Formula_With_Text()
Dim linkWS As Worksheet, newWS As Worksheet
Dim linkText$, url$, address$, subPart$
Dim cel As Range, rng As Range
Dim commaPos&

Set linkWS = Sheets("Sheet1") ' Change this to the sheet name that has =Hyperlink("http://...","Link")
Set newWS = Sheets.Add
newWS.Name = "Links Only" ' or whatever you want the non-Hyperlink Formula sheet to be called.

Set rng = linkWS.Range("A1:A2") ' Edit this as needed

For Each cel In rng
    linkText = cel.Value
    commaPos = InStrRev(cel.Formula, ",")

    subPart = WorksheetFunction.Substitute(linkWS.Range("A2"), " ", "+")
    url = Mid(cel.Formula, WorksheetFunction.Search("(", cel.Formula) + 2, commaPos)
    Debug.Print url

    commaPos = InStrRev(url, ",")
    url = Left(url, commaPos - 2)
    url = WorksheetFunction.Substitute(url, "&", ";", 1)
    Debug.Print url


    ' Now, trim SUBSTITUTE out of it
    Dim leftUrl$, rightURL$
    leftUrl = Left(url, WorksheetFunction.Search(";", url) - 2)
    rightURL = Mid(url, WorksheetFunction.Search("&", url) + 2, Len(url))
    url = leftUrl & subPart & rightURL
    Debug.Print url
  '  cel.ClearContents
    address = cel.address
    newWS.Range(address).Hyperlinks.Add anchor:=newWS.Range(address), address:=url, TextToDisplay:=linkText
Next cel

End Sub

它是克服的,但它应该成功解析&Substitute...&部分,并将其替换为A2中的内容。