我在網上發現了很多東西,告訴我如何創建函數以及如何實現它,但沒有什麼能幫助我找出爲什麼它不接受函數的名字。正則表達式產生#NAME? UDF中的錯誤
我打開開發人員的Visual Basic部分。我輸入了我的代碼,我認爲是嗎? Ctrl + S只能讓我保存表格,而不是代碼。
我的代碼的目的是取一個字符串,並刪除前7個字符,其中之一將是一個;以下6個將是隨機數字。我還有更多的修復工作要做,比如從最後刪除4個隨機字符,但我想先測試一下。
這裏是我的代碼:
Function simpleCellRegex(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 = "^[;][0-9]{6}"
If strPattern <> "" Then
strInput = Myrange.Value
strReplace = ""
With regEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = strPattern
End With
If regEx.test(strInput) Then
simpleCellRegex = regEx.Replace(strInput, strReplace)
Else
simpleCellRegex = "Not matched"
End If
End If
End Function
我不知道如果有,我很想念,讓Excel來接受我的碼的步驟。
感謝您的幫助!
您是否試圖從另一個宏調用您的函數傳遞一個範圍?這可能有助於調試。如果您的功能不受支持,則很可能包含錯誤。 – Fuzzzzel
您是否將模塊添加到工作簿並在其中定義該功能?在工作表中定義的函數不起作用 –
@Fuzzzzel我實際上從本網站高度投票的線程中獲取了整個函數 - http://stackoverflow.com/questions/22542834/how-to-use-regular-expressions-regex-in -Microsoft-Excel的兩種功能於細胞和循環。 – Ryan