我想创建一个宏或UDF,它可以在excel工作表中找到包含以下内容的单元格: POxxx PO xxxxxxx PO#xxxxx PO#XXXX (x为数字) 字符串可以位于单元格的开头或中间。 此外,函数/宏不应该找到包含像CORPORATE这样的条目的单元格,其中PO是单词的一部分。
应突出显示包含合格数据的所有单元格。
答案 0 :(得分:0)
这个小 UDF 将返回 1 匹配是否存在,否则 0
Public Function IsItThere(r As Range) As Long
Dim st As String
st = "0,1,2,3,4,5,6,7,8,9"
ary = Split(st, ",")
st = r.Text
IsItThere = 1
For Each a In ary
If InStr(1, st, "PO" & a) > 1 Then Exit Function
If InStr(1, st, "PO " & a) > 1 Then Exit Function
If InStr(1, st, "PO#" & a) > 1 Then Exit Function
If InStr(1, st, "PO# " & a) > 1 Then Exit Function
Next a
IsItThere = 0
End Function
您也可以使用正则表达式来查找模式。
答案 1 :(得分:0)
试试这个:
Sub Tester()
Dim c As Range
For Each c In Selection.Cells
c.Interior.Color = IIf(RegexpTest(c.Value), vbRed, vbGreen)
Next c
End Sub
Function RegexpTest(v As String)
Static re As Object 'note static: you must reset the VB environment
' (press the "stop" button) if you edit the
' Pattern below
If re Is Nothing Then
Set re = CreateObject("VBScript.RegExp")
'"PO" then optional #, optional space, then 2-5 digits
re.Pattern = "PO#?\s?\d{2,5}"
re.ignorecase = True
End If
RegexpTest = re.test(v)
End Function