使用正则表达式“未设置对象变量或带块变量”

时间:2018-11-21 13:24:19

标签: regex excel vba excel-vba

我遇到错误

  

“对象变量或未设置块变量”

将功能设置为RegEx对象,所以事情是我需要使用这些模式来验证文件名。

我认为Select case也是错误的。我不知道该怎么解决。

Do While xExcelFile <> ""        
    strPattern = GetPattern(newFileName)

    With RegEx
        .Pattern = strPattern 'error occurs here
    End With


    Select Case True
        'go to next loop if the selected file and this workbook has the same name
        Case xExcelFile = xWname
            GoTo NextLoop

        'filter by name pattern
        Case RegEx.Test(newFileName)
            Workbooks.Open fileName:=xSPath & xExcelFile 'open file

        Case RegEx.Test(newFileName)
            Workbooks.Open fileName:=xSPath & xExcelFile 'open file`

more operational code below

这是搜索所需模式的功能,我认为它可以正常工作

Function GetPattern(ByVal newFileName As String) As String
Dim strPattern As String
Static RegEx As Object

strPattern1 = "^([B]\d{3}\w{1,})"
strPattern2 = "^\d*\.\d{4}\w*"

If RegEx Is Nothing Then
   ' To speed the code up set the static RegEx object only once
   Set RegEx = CreateObject("VBScript.RegExp")
End If

With RegEx
    .Pattern = strPattern1
    If RegEx.Test(newFileName) Then
        strPattern = strPattern1
    End If

    .Pattern = strPattern2
    If RegEx.Test(newFileName) Then
        strPattern = strPattern2

    End If
End With

GetPattern = strPattern

End Function

1 个答案:

答案 0 :(得分:0)

您的I'm searching in File 1 record; file Publisher1 I'm searching in File 2 file Publisher 2; Publisher 2A 没有道理。

应该是

Case RegEx.Test(newFileName)