感 谢 关 注 导读 这两天有朋友问我如果要只保留数字或只保留字母该怎么办?所以今天村长再来补充一下其他正则表达式在VBA中的应用。 按ALT F11弹出VBE窗口,然后新建模块,如下图: 只保留中文的代码如下: Function ZW(i As String) As String Dim a As Object Set a = CreateObject('VBSCRIPT.REGEXP') a.Pattern = '[^\u4e00-\u9fa5]' a.IgnoreCase = True a.Global = True ZW = a.Replace(i, '') Set a = Nothing End Function 只保留数字的代码如下: Function SZ(i As String) As String Dim a As Object Set a = CreateObject('VBSCRIPT.REGEXP') a.Pattern = '[^0-9]' a.IgnoreCase = True a.Global = True SZ = a.Replace(i, '') Set a = Nothing End Function 只保留字母的代码如下: Function ZM(i As String) As String Dim a As Object Set a = CreateObject('VBSCRIPT.REGEXP') a.Pattern = '[^A-Z^a-z]' a.IgnoreCase = True a.Global = True ZM = a.Replace(i, '') Set a = Nothing End Function 只保留数字和字母的代码如下: Function SZZM(i As String) As String Dim a As Object Set a = CreateObject('VBSCRIPT.REGEXP') a.Pattern = '[^A-Z^a-z^0-9]' a.IgnoreCase = True a.Global = True SZZM = a.Replace(i, '') Set a = Nothing End Function 保存完代码之后我们回到表格里面,在单元格输入我们刚才写的自定义函数,如下图: 坚持!坚持!再坚持! 致各位亲 |
|