正则expression式产生#NAME? UDF错误
我在网上发现了很多东西,告诉我如何创build函数以及如何实现,但没有什么能帮助我找出为什么它不接受函数的名字。
我打开了开发人员的Visual Basic部分。 我input了我的代码,我认为是这样吗? Ctrl + S只能让我保存表格,而不是代码。
我的代码的目的是采取一个string,并删除前7个字符,其中之一将是一个; 以下6个将是随机数。 我有更多的修复方法,比如从最后删除4个随机字符,但是我想先testing一下。
这是我的代码:
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接受我的代码。
谢谢你的帮助!
看看这个线程。 您很可能错过了添加对Microsoft VBScript Regular Expressions 5.5
的引用。 您可以在“如何使用”下find它:
如何在Microsoft Excel中使用正则expression式(正则expression式)在单元格内和循环中
我修改了下面的代码来清理variables并使用后期绑定
此外,您当前的代码不会testing用户使用多个单元格进入范围。
Function simpleCellRegex(Myrange As Range) As String Dim regEx As Object Dim strPattern As String Dim strReplace As String Set regEx = CreateObject("vbscript,regexp") strPattern = "^[;][0-9]{6}" If Len(strPattern) = 0 Then Exit Sub simpleCellRegex = "Not matched" strReplace = vbNullString With regEx .Global = True .MultiLine = True .IgnoreCase = False .Pattern = strPattern If .test(Myrange) Then simpleCellRegex = .Replace(Myrange.Value2, strReplace) End With End Function