这里在excel表中我循环遍历列(包含URL)提取模式(例如.us / en / dhs)但是我收到运行时错误:5 尝试了一段类似的代码,但这里失败了。
包含粗体文字的行出错。
Sub generateLWP() ' ' In the Active Sheet : ' - Extract the LWP from the URL and save it in column N ' - Identify pattern in every URL in the From Column and add the matching word to its corresponding cell in Article column ' 'Select Non-Blank Link Text ‘ Call selectNonBlank 'Start from D3(URL) and loop through till the end and extract the LWP pattern With ActiveSheet .Range("N1") = "LWP" '.Range("O1") = "Article Pattern" .Range("D3").Select End With 'End 'Variables Dim lwp As String: lwp = "" Dim str As String Set regEx = CreateObject("vbscript.regexp") With regEx .Global = True '.MultiLine = True .IgnoreCase = True .pattern = "/([a-zA-Z]){2}\/([a-zA-Z]){2}\/(([a-zA-z]{5}[+0-9])|[a-zA-z]{3}|[0-9]{2})/g" End With Do Until IsEmpty(ActiveCell) 'extract the LWP pattern and store it in corresponding cell of column N str = ActiveCell.Value Set regMatchObj = regEx.Execute(str) ‘Object containing the matching text lwp = regMatchObj.Item(0) ‘lwp = regMatchObj.Item(0).SubMatches.Item(0) ActiveCell.Offset(0, 10).Text = lwp ActiveCell.Offset(1, -10).Select 'Go down Loop End Sub
你能帮忙确定我在这条线上收到错误的原因吗?
答案 0 :(得分:1)
lwp
是Match
对象,而不是字符串,因此您需要更改声明并在分配值时使用Set。
请参阅 https://msdn.microsoft.com/en-us/library/yfy6y4h1(v=vs.84).aspx