一张图片解决你所有问题,细看
Function 替换(检测内容) As String
Dim regx As Object '后期绑定
Set regx = CreateObject("VBScript.RegExp") '后期绑定
Dim mat As Object
Dim m As Object
Dim 返回内容 As String
Set regx = New RegExp
With regx
.Global = True
'如果检测内容中含有非字母和非数字则返回"失败"
.Pattern = "\W"
Set mat = .Execute(检测内容)
If mat.Count <> 0 Then
替换 = "失败"
Exit Function
End If
'替换字线为A,数字为0,单个字母替换,返回替换后的字符
.Pattern = "[a-zA-Z]"
返回内容 = .Replace(检测内容, "A")
.Pattern = "[0-9]"
替换 = .Replace(返回内容, "0")
End With
End Function
Sub test()
Cells(2, "B") = 替换(Cells(1, "B"))
Cells(2, "C") = 替换(Cells(1, "C"))
End Sub