自己建立 自定义函数 ,alt+f11 打开VBA 编辑器 插入一个模块, 粘贴下面代码 ,在EXCEL 表里面就可以使用函数 getstr() 来提取了
参数1:选单元格
参数2: “sz” 代表数字 ,“zm” 代表字母 ,“hz” 代表 汉字,"dh" 代表 电话号码
参数3:1 代表替换, 2代表提取 ,参数3可以省略 默认 是提取
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/1b4c510fd9f9d72a32dfdf56d72a2834359bbbc6?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
Public Function getstr(rg As String, k As String, Optional j As Integer = 1)
Dim regx As New RegExp
With regx
.Global = True
If k = "sz" Or k = "SZ" Then
.Pattern = "\d" '数字
ElseIf k = "hz" Or k = "HZ" Then
.Pattern = "[\u4e00-\u9fa5]" ' 汉字
ElseIf k = "zm" Or k = "ZM" Then
.Pattern = "[A-z]" '字母
ElseIf k = "dh" Or k = "DH" Then
.Pattern = "\d{3,4}\-\d{7,8}|\d{11}|\d{8}" '电话号码
End If
If j = 2 Then '替换
getstr = .Replace(rg, "")
ElseIf j = 1 Then '提取
m1 = ""
Set mat = .Execute(rg)
For Each m In mat
m1 = m1 & m
Next
getstr = m1
End If
End With
End Function
追问看不懂
追答浪费表情