0

您好,我在使下面的正则表达式代码正常工作时遇到了一些问题。我收到应用程序未定义或对象未定义错误

文本框在用户表单上。

错误发生在“Set allMatches = regEx.Execute(TextBox1.Text)”行上,不确定我错过了什么。

Dim regEx As Object

Dim allMatches As Object

Set regEx = CreateObject("VBScript.RegExp")
With regEx
        .IgnoreCase = True
        .MultiLine = False
        .Pattern = "\d{2,2}/\d{2,2}/\d{4,4} \d{2,2}:\d{2,2)[AM|PM]"
        .Global = True
End With

Set allMatches = regEx.Execute(TextBox1.Text)


If allMatches.Count <> 0 Then
    result = allMatches.Item(0).submatches.Item(0)
End If
4

3 回答 3

1

好的,经过一番谷歌搜索,我发现了问题:它的模式:

 .pattern = "\d{2,2}/\d{2,2}/\d{4,4} \d{2,2}:\d{2,2**)** [AM|PM]"

事实证明,如果模式无效,您将收到 5017 错误。

通过将“)”更改为正确的关闭“}”,错误得到解决。

 .pattern = "\d{2,2}/\d{2,2}/\d{4,4} \d{2,2}:\d{2,2**}** [AM|PM]"

我会认为如果模式不匹配,那么你会得到一个错误的回报,不是这样..

于 2013-08-21T01:19:14.710 回答
0

因此,您可能错误地访问了文本框。在不知道如何设置文本框的情况下,我猜测它保存在形状集合中。您可以查找文本框,然后设置 allMatches,如下所示

Dim shp As Shape
'loop through the shapes on the sheet - assuming you are working with sheet 1
For Each shp In ThisWorkbook.Sheets(1).Shapes
    If shp.Name = "TextBox1" Then
         Set allMatches = regEx.Execute(shp.TextFrame2.TextRange.Text)
    End If
Next
于 2013-08-20T22:08:42.523 回答
0

假设您正确访问文本框,这[AM|PM]在我看来是个问题。
在我看来,这将匹配一个字符:A、M 或 P,或 M。

如果是我,我会使用[A|P]M-第一个字母可以是 A 或 P,第二个字母必须是 M。
这也假设它只是在寻找大写字母。
包括小写,([Aa]|[Pp])[Mm]或者可能更好[AaPp][Mm]

于 2021-03-12T17:45:25.390 回答