I have been struggling with this issue for a couple of days and have basically been unable to find a solution
I have written a function in VBA that should return the result from a RegEx.Execute statement but every time the 'End Function' statement is reached, I get a Runtime Error 450 and I simply cannot figure where things go wrong since my assigning of object variables does not throw an error.
I am new to VBA and programming altogether so there is probably just something I have misunderstood in the way objects/MatchCollection/Collections behave.
Here is my code:
Function FindDateAndTimePatternRegEx(s As String) As Object
On Error GoTo erro
Dim objRex As RegExp
Dim arrMatch As Object
's = " Afgår 01-01-2017 00:24:00, anvendt log 01-01-2017 10:53:24 er 10:29:24 EFTER dette"
Set objRex = New RegExp
With objRex
.IgnoreCase = True
.Global = True ' the global parameter makes sure that all pattern matches in the string are found and returned in the return collection
.Pattern = "([0-9]{2}\-[0-9]{2}\-[0-9]{4})" & " " & "([0-9]{2}:[0-9]{2}:[0-9]{2})"
End With
Set arrMatch = objRex.Execute(s)
Set FindDateAndTimePatternRegEx = arrMatch
'Debug.Print FindDateAndTimePatternRegEx.Item(0).FirstIndex
'Debug.Print FindDateAndTimePatternRegEx.Item(1)
Set objRex = Nothing
Exit Function
erro:
MsgBox Err.Description
End Function
Everytime I try this out in the Immediate window calling the function and using the string s
? ?FindDateAndTimePatternRegEx(" Afgår 01-01-2017 00:24:00, anvendt log 01-01-2017 10:53:24 er 10:29:24 EFTER dette")
the debug.print returns the expected values but when either Exit Function or End Function is reached, I get the runtime error 450
Could anybody explain what goes wrong?
Thanks in advance
Best regards