我想創建我自己的VBA函數基於在這個鏈接第二步中的一個。 How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops正則表達式提取#名稱錯誤
但是,我收到#NAME錯誤。我在哪裏錯了?我已在https://regex101.com上測試過我的RegEx。
Function extractGroupName(Myrange As Range) As String
Dim regEx As New RegExp
Dim strPattern As String
Dim strInput As String
Dim strReplace As String
Dim strOutput As String
strPattern = "^.*Name:(.*);Id"
If strPattern <> "" Then
strInput = Myrange.Value
strReplace = "$1"
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.Test(strInput) Then
extractGroupName = regEx.Replace(strInput, "$1")
Else
extractGroupName = "ERROR: NOT FOUND"
End If
End If
End Function
你打電話此函數來自工作表單元格,並且單元格中的返回值是#NAME?如果是這樣的話,你把代碼放在哪裏?它通常應在同一工作簿中的常規模塊中。您是否將該模塊命名爲與該功能相同的名稱?此外,您是否在工具/參考下設置了對正則表達式的引用? –
是的,我從工作表單元格中調用它,並且單元格中的返回值是#NAME。 代碼位於工作簿中的模塊中。 模塊和函數具有相同的名稱。 我已經打開了對正則表達式的引用。 當我複製粘貼我提供的逐字逐句的鏈接的第2步中的代碼時,一切正常,但是當我這樣做時,它會導致#NAME錯誤,這意味着它是代碼問題,而不是命名模式,位置或引用。 – Haybuck
你如何以及在哪裏放置VBA代碼? –