在Excel中search并replace超链接中的某些扩展

我有一个超链接负载的Excel文件。 一些超链接匹配某种模式,可以说file:///\\location\file_name_<xxx>.pdf ,其中<xxx>变化。 还有其他的超链接是相似的,比如file:///\\location\other_file_<xxx>.pdf

我希望能够search和replace所有匹配的超链接(使用简单的*通配符来解释) *file_name*.pdf并replace*file_name*.mht

因此,任何指向PDF匹配file_name_<xxx>超链接都会将其扩展名replace为mht

您可以使用开发人员选项卡下的Visual Basic使用正则expression式。 你必须做相当多的编码。 我会提供我自己的解释,但是这个问题的答案已经在另一个stackoverflow问题中相当彻底地解释。 点击这里>>( 如何在Microsoft Excel中使用正则expression式(正则expression式)in-cell和loops )

综上所述:

步骤1:将VBA引用添加到“Microsoft VBScript Regular Expressions 5.5”

第2步:定义你的模式

步骤3:作为macros运行,作为内嵌函数运行,或循环遍历

我相信这个例子对你最有帮助的是例4:

 Private Sub splitUpRegexPattern() Dim regEx As New RegExp Dim strPattern As String Dim strInput As String Dim strReplace As String Dim Myrange As Range Set Myrange = ActiveSheet.Range("A1:A3") For Each C In Myrange strPattern = "(^[0-9]{3})([a-zA-Z])([0-9]{4})" If strPattern <> "" Then strInput = C.Value strReplace = "$1" With regEx .Global = True .MultiLine = True .IgnoreCase = False .Pattern = strPattern End With If regEx.test(strInput) Then C.Offset(0, 1) = regEx.Replace(strInput, "$1") C.Offset(0, 2) = regEx.Replace(strInput, "$2") C.Offset(0, 3) = regEx.Replace(strInput, "$3") Else C.Offset(0, 1) = "(Not matched)" End If End If Next End Sub