【发布时间】:2018-03-21 22:19:04
【问题描述】:
我需要在两列中执行两个不同的验证。在“C”列中我需要检查特殊字符,在“D”列中我需要检查是否有数字。
有没有我可以在 VBA 中使用正则表达式来实现这一点?
提前感谢您的帮助
Dim strPattern As String: strPattern = "[^a-z]"
Dim regEx As Object
Set regEx = CreateObject("VBScript.RegExp")
regEx.Global = True
regEx.IgnoreCase = True
regEx.Pattern = strPattern
For Each cell In ActiveSheet.Range("C:C") ' Define your own range here
If strPattern <> "" Then ' If the cell is not empty
If regEx.Test(cell.Value) Then ' Check if there is a match
cell.Interior.ColorIndex = 6 ' If yes, change the background color
End If
End If
Next
【问题讨论】:
-
您可以使用
WildCards和Like Operator -
Is there anyway I can achieve this using regEx in VBA?- 是的。如需更多帮助,请阅读how to ask -
Dim strPattern As String: strPattern = "[^az]" Dim regEx As Object Set regEx = CreateObject("VBScript.RegExp") regEx.Global = True regEx.IgnoreCase = True regEx.Pattern = strPattern For Each cell In ActiveSheet.Range("C:C") ' 在这里定义自己的范围 If strPattern "" Then ' 如果单元格不为空 If regEx.Test(cell.Value) Then ' 检查是否有a match cell.Interior.ColorIndex = 6 ' 如果是,则更改背景颜色 End If End If Next
-
这是我当前代码的外观,仅取自本网站。现在我需要在 D 列上申请以验证同一脚本验证中的数字。
-
@Rick 如果你使用负匹配
[^A-Za-z0-9],它可以检测特殊字符,但是,它会检测`´~çÇàáé,你能指定特殊字符吗?或者一定是all of them?