我有以下代码,由于显示“对象'IRegExp2的方法'执行'失败”的错误消息,它没有执行
Dim wrksht As Worksheet
Dim pg As Page
Dim regEx As New VBScript_RegExp_55.RegExp
For Each wrksht In Worksheets
For Each pg In wrksht.PageSetup.pages
With regEx
.IgnoreCase = True
.Pattern = "[(ftp|FTP|http|HTTP|s|S)]{3,5}*[\:/]{3}*[a-zA-Z0-9\s]{2,}[\.]*[.A-Za-z0-9\s]{2,}"
End With
If regEx.Execute(pg.CenterFooter.Text) Then
pg.CenterFooter.Text = regEx.Replace(pg.CenterFooter.Text, "")
End If
Next pg
Next wrksht
我正在寻找替换网址的开头部分( www.sampleurl.com /subfolder/testfile.pdf)并仅保留尾随部分(www。 sampleurl.com /subfolder/testfile.pdf ,减去.com部分后面的空格)
答案 0 :(得分:1)
比替换.com(每条评论)稍微更通用......希望这可以让你到达你需要的位置:
Dim LR As Long
LR = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row
For i = 1 To LR
Cells(i, 1) = Replace(Cells(i, 1), "https://", "", , 1)
Cells(i, 1) = Replace(Cells(i, 1), "http://", "", , 1)
Cells(i, 1) = Replace(Cells(i, 1), "/", "(error)", , 1)
Next i
Range(Cells(1, 1), Cells(LR, 1)).Replace What:="*(error)", Replacement:=""
我的假设是有一个列中包含地址。
答案 1 :(得分:0)
我从https://stackoverflow.com/a/6041965/5947891复制了正则表达式,以下是适用于我的最终版本。基本上,这将扫描工作簿中的所有工作表,检查URL模式,截断URL基础( http://www.sample.com /subfolder/subfile.png)并保留尾随部分(s)(例如,http://www.sample.com**/subfolder/subfile.png**)。我在URL中的http之后添加了空格以使href无效。
Sub FixFooter()
Dim oSection As Word.Section
Dim oRange As Word.Range
Dim var
Dim wrksht As Worksheet
Dim pg As Page
Dim regEx As New RegExp 'New VBScript_RegExp_55.RegExp
Dim strPattern As String
strPattern1 = "(http|ftp|https){0,1}(\://){0,1}([\w_-]+(?:(?:\.[\w_-]+)+))/"
For Each wrksht In Worksheets
Set pg = wrksht.PageSetup.pages(1)
With regEx
.IgnoreCase = True
.Global = True
.MultiLine = True
.Pattern = strPattern1
End With
If regEx.Test(pg.CenterFooter.Text) Then
pg.CenterFooter.Text = "/" & regEx.Replace(pg.CenterFooter.Text, "")
End If
Next wrksht
End Sub