以下代码是我迄今为止提出的。我还为正则表达式5.5设置了引用。
然而,匹配值根本没有显示,我在这里遗漏了什么吗?
Public Sub check()
Dim wb As Workbook
Set wb = ThisWorkbook
'Prepare a regular expression object
Dim myRegExp As New VBScript_RegExp_55.RegExp
Dim myMatches As MatchCollection
Dim myMatch As match
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "^\d{6,8}-[SFTG]\d{7}[A-Z]-([^-]+)$"
Set myRegExp = CreateObject("vbscript.regexp")
cellValue = CStr(wb.Worksheets(1).Cells(2, 4).Value)
'123456-S1234567F-Scholarship Form
If myRegExp.Test(cellValue) Then
Set myMatches = myRegExp.Execute(cellValue)
For Each myMatch In myMatches
MsgBox (myMatch.Value)
Next
Else
End If
End Sub
答案 0 :(得分:1)
删除以下行
Set myRegExp = CreateObject("vbscript.regexp")
似乎解决了这个问题。
我想这是因为当您将myRegExp
分配给新对象时,所有初始化语句都消失了