【发布时间】:2026-02-15 21:40:01
【问题描述】:
我正在为此失去理智,我知道这比我做的要简单得多。
我希望用被相同文本屏蔽的超链接简单地替换多次出现的文本。
场景: 由于 Covid-19,我们正在迁移到 Google 课堂,我们正在考虑提前到 9 月。我可以将学生的时间表批量导出为 HTML 文件(让我们将此文件称为时间表)。 我有一个单独的文件,其中列出了所有班级名称和 Google 教室的链接(让我们称之为一个班级)。我已将这些值复制到以下代码的时间表文件的“Sheet1”中。
如果我可以使用课程列表搜索时间表中出现的所有课程,然后将它们替换为指向 google 教室的超链接,但我希望它显示为课程名称。
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
感谢您的帮助。
【问题讨论】:
-
它只是插入没有超链接的文本吗?
-
是的,它只是将链接作为文本插入。我希望它插入可点击的链接,但我想显示为类名的文本。我正在尝试 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