0

我想创建一个宏或 UDF,它可以在包含以下内容的 excel 工作表中找到单元格: POxxx PO xxxxxxx PO# xxxxx PO#xxxx(其中 x 是数字)字符串可能位于单元格的开头或中间. 此外,函数/宏不应找到包含诸如 CORPORATE 之类的条目的单元格,其中 PO 是单词的一部分。

所有包含合格数据的单元格都应突出显示。

4

2 回答 2

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
于 2015-01-19T17:51:50.297 回答
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

您还可以使用正则表达式来查找模式。

于 2015-01-15T19:14:31.897 回答