实例化“匹配”对象时,VBA崩溃

时间:2015-06-17 13:11:35

标签: vba

我很困惑为什么下面的代码与''< -----“一起崩溃。
话虽如此,我没有得到任何错误消息,VBE设置为中断所有错误,不仅仅是未处理,也没有时间编写错误处理程序。

还有一个事实是,这是一个来自更大的类模块的过程,当代码遇到中断时,我尝试在Locals窗口中扩展Me整个应用程序(Excel)崩溃。

此类模块中的所有其他方法都可以正常工作,下面列出了与有问题的过程相关的所有变量。 相关变量:

VectorPatterns(1) = "\d+\sramp\s\d+\s"
PatternList( 1 To 1) = ( "\d+\sramp\s\d+\s" )
Text = "5 ramp 12 "
Ahead = 0

有问题的代码

Private Function ReadNext(ByRef Text As String, Optional Ahead As Integer = 0) As FoundPattern
    Dim FoundItems As VBScript_RegExp_55.MatchCollection
    Dim FoundItem As VBScript_RegExp_55.Match
    Dim APattern As Integer
    Dim FoundPattern As FoundPattern
    Dim PatternLookbehind As String
    Dim DemitedPatternList As String


    DemitedPatternList = Join(PatternList, "|")

    If Ahead > 0 Then
        PatternLookbehind = _
            "^(?<=" & _
            DemitedPatternList & _
            "){" & Ahead & "}"
    Else
        PatternLookbehind = "^"
    End If

    For APattern = 1 To PatternCount
        Regex.Pattern = PatternLookbehind & VectorPatterns(APattern).Regex
        Set FoundItems = Regex.Execute(Text)
        If FoundItems.Count > 0 Then
            Set FoundItem = FoundItems.Item(1)                '<-----
            FoundPattern.Type = VectorPatterns(APattern).Type
            FoundPattern.Value = FoundItem.Value
        End If
    Next APattern

    ReadNext = FoundPattern
End Function

1 个答案:

答案 0 :(得分:2)

FoundItems.Item(i)从零开始。这意味着第一项是i = 0,第二项是i = 1等。

代码中的问题出现在FoundItems.Count = 1时。您必须使用Set FoundItem = FoundItems.Item(0)返回第一项。

更多this tutorial