首页 文章

在VBA中从URL发出提取模式

提问于
浏览
0

在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

你能帮忙确定我在这条线上收到错误的原因吗?

1 回答

相关问题