【问题标题】:MS Excel Find text and replace with HyperlinkMS Excel 查找文本并替换为超链接
【发布时间】:2026-02-15 21:40:01
【问题描述】:

我正在为此失去理智,我知道这比我做的要简单得多。

我希望用被相同文本屏蔽的超链接简单地替换多次出现的文本。

场景: 由于 Covid-19,我们正在迁移到 Google 课堂,我们正在考虑提前到 9 月。我可以将学生的时间表批量导出为 HTML 文件(让我们将此文件称为时间表)。 我有一个单独的文件,其中列出了所有班级名称和 Google 教室的链接(让我们称之为一个班级)。我已将这些值复制到以下代码的时间表文件的“Sheet1”中。

如果我可以使用课程列表搜索时间表中出现的所有课程,然后将它们替换为指向 google 教室的超链接,但我希望它显示为课程名称。

This is an image of the timetable file. This is one timetable but the file continues the same pattern for different pupils

This is an image of the classlist file. Imagine there were all classes listed and links were valid.

我一直在尝试找到的代码,但无法在“替换”元素中获得有效的超链接。

'PURPOSE: Find & Replace a list of text/values throughout entire workbook from a table
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

Dim sht As Worksheet
Dim fndList As Integer
Dim rplcList As Integer
Dim tbl As ListObject
Dim myArray As Variant

'Create variable to point to your table
  Set tbl = Worksheets("Sheet1").ListObjects("Table2")

'Create an Array out of the Table's Data
  Set TempArray = tbl.DataBodyRange
  myArray = Application.Transpose(TempArray)

'Designate Columns for Find/Replace data
  fndList = 1
  rplcList = 2

'Loop through each item in Array lists
  For x = LBound(myArray, 1) To UBound(myArray, 2)
    'Loop through each worksheet in ActiveWorkbook (skip sheet with table in it)
      For Each sht In ActiveWorkbook.Worksheets
        If sht.Name <> tbl.Parent.Name Then
          sht.Cells.Replace What:=myArray(fndList, x), Replacement:=myArray(rplcList, x), _
          LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
          SearchFormat:=False, ReplaceFormat:=False
        End If
      Next sht
  Next x

End Sub

感谢您的帮助。

3:EDIT: Example timetable file

【问题讨论】:

  • 它只是插入没有超链接的文本吗?
  • 是的,它只是将链接作为文本插入。我希望它插入可点击的链接,但我想显示为类名的文本。我正在尝试 vba Hyperlink.add 但对我不起作用。
  • 您使用的超链接代码是什么,因为这似乎是核心问题?您是否尝试过录制插入链接的宏?实际上你可能不需要查找和替换,你不能添加链接吗?
  • Hyperlinks.Add Anchor:=.Range("a5"), _ Address:="https://example.microsoft.com", _ ScreenTip:="Microsoft Web Site", _ TextToDisplay:="Microsoft" 这是代码的格式。问题发生在 Anchor 中,我不知道该使用什么。我使用的地址是 myArray(rplcList, x),要显示的文本是 myArray(fndList, x)。我不确定我是否关注你只是添加链接。该文件包含大约 246 个时间表,包含 100 多个不同的课程。
  • Worksheets(1).Hyperlinks.Add ...

标签: excel vba replace hyperlink


【解决方案1】:

试试这个。该链接似乎需要完整的地址,包括“https”。

我模拟了一个包含两列的表格和一些要测试的值。

Sub x()

Dim r As Range, t As ListObject, rFind As Range, s As String

With Worksheets("Sheet1")
    Set t = .ListObjects("Table1")
    For Each r In t.ListColumns(1).DataBodyRange 'loop through first column of table
        Set rFind = .Range("A:C").Find(What:=r.Value, Lookat:=xlWhole, MatchCase:=False, SearchFormat:=False) 'look for value
        If Not rFind Is Nothing Then
            s = rFind.Address 'store address of first cell
            Do
                .Hyperlinks.Add Anchor:=rFind, Address:=r.Offset(, 1).Value, SubAddress:="", ScreenTip:=r.Offset(, 1).Text, TextToDisplay:=r.Value 'add hyperlink
                Set rFind = .Range("A:C").FindNext(rFind) 'look for next instance
            Loop While rFind.Address <> s 'keep going until back to first case
        End If
    Next r
End With

End Sub

之前

之后

链接表

【讨论】:

  • 我无法让它适用于我的布局,但如果我按照您在此处使用测试数据的方式设置它,它将起作用。感谢您的时间。我在 OP 中包含了我的时间表示例文件。
  • 到底是什么问题。
  • 没有出现错误。然而,我今天早上用一双新鲜的眼睛试了几次。问题似乎出在时间表数据中。即使我将数据复制到新的 excel 文件,它也不起作用。如果我在现有的文本上手动输入相同的文本,它就可以工作。即使文本看起来相同,它看起来也不是。
  • 您可以尝试Lookat:=xlpart,但可能会失败,具体取决于您的条目的独特程度。
  • 我发现了问题。我知道我在导出的数据中有尾随空格,所以我在类列表数据中允许这样做。导出的数据使用 chr(160) 作为空格,显然我使用的是 chr(32) 空格键。