我一直在這個問題掙扎了幾天,已經基本無法找到一個解決方案返回正則表達式MatchCollection從功能VBA - 錯誤450
我已經寫在VBA函數應該返回的結果從RegEx.Execute語句,但每次達到'結束函數'語句時,我得到一個運行時錯誤450,我根本無法確定事情出錯的地方,因爲我分配的對象變量不會引發錯誤。
我是VBA和編程的新手,所以可能只是我誤解了objects/MatchCollection/Collections的行爲方式。
這裏是我的代碼:
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
每次我嘗試了這一點立即窗口調用該函數,並使用字符串s
? ?FindDateAndTimePatternRegEx(" Afgår 01-01-2017 00:24:00, anvendt log 01-01-2017 10:53:24 er 10:29:24 EFTER dette")
的debug.print返回預期值,但是當要麼退出功能或結束功能,我得到運行時錯誤450
任何人都可以解釋什麼錯了?
在此先感謝
問候
感謝Wiktor的 - 這解決了我的問題! –
對不起,當然 - 我現在已經接受了答案。仍在學習如何瀏覽論壇 –