Function zhengze(ze As String, Rng As Range)
Set regx = CreateObject("vbscript.regexp")
With regx
.Global = True
.Pattern = ze '写正则表达式
Set mat = .Execute(Rng)
For Each mg In mat
If .test(Rng) Then
zhengze = mg
Else
zhengze = 0
'Set mat = .Execute(value)
'cells 单元格 读取单元格内容,将内容写入单元格,将变量rg的值输入到c列的n行
End If
Next
End With
End Function
------------------------------------------------------------------------------------------------------------------------------------
if外置,无匹配值则返回空值,单引号后为注释内容,可忽略
-------------------------------------------------------------------------------------------------------------------------------------
Function zhengze(ze As String, Rng As Range)
Set regx = CreateObject("vbscript.regexp")
With regx
.Global = True
.Pattern = ze '写正则表达式
Set mat = .Execute(Rng)
If .test(Rng) Then '无匹配值则为空
For Each mg In mat
'If .test(Rng) Then
zhengze = mg
'Set mat = .Execute(value)
'cells 单元格 读取单元格内容,将内容写入单元格,将变量rg的值输入到c列的n行
'End If
Next
Else
zhengze = " "
End If
End With
End Function
------------------------------------------------------------------------------------------------------------------------------------
匹配结果多个时,合并显示
==================================================================================
Function zhengze(ze As String, Rng As Range)
Set regx = CreateObject("vbscript.regexp")
With regx
.Global = True
.Pattern = ze '写正则表达式
Set mat = .Execute(Rng)
If .test(Rng) Then '无匹配值则为空|匹配成功执行循环
Dim m As String
If mat.Count > 1 Then '为多个匹配结果则合并显示,否则显示当前值
For Each mg In mat
m = m & mg & "|" '循环并合并匹配结果
Next
zhengze = m
Else
zhengze = mat(0).value '参数存储是一个列表形式,不能直接=号取值,必须用列表固有取值方式
End If
Else
zhengze = " "
End If
End With
End Function