|
- Sub CheckHasKey()
- Dim sh As Worksheet, lngRow As Long, arr As Variant
- Dim objReg As Object, strTemp As String, strPat As String
-
- strPat = GetPat
- If strPat = "" Then Exit Sub
-
- Set sh = Sheets("Sheet1")
- lngRow = sh.Range("A" & Rows.Count).End(xlUp).Row
- If lngRow < 2 Then Exit Sub
-
- sh.Range("A1:A" & lngRow).Interior.ColorIndex = 0
- arr = sh.Range("A1:A" & lngRow)
- Set objReg = CreateObject("VBScript.RegExp")
- With objReg
- .Global = True
- .Pattern = strPat
- End With
-
- For lngRow = LBound(arr) + 1 To UBound(arr)
- strTemp = CStr(arr(lngRow, 1))
- '不含,背景色标红
- If Not objReg.test(strTemp) Then sh.Range("A" & lngRow).Interior.ColorIndex = 3
- Next
-
- Set objReg = Nothing
- Set sh = Nothing
- End Sub
- Function GetPat() As String
- Dim sh As Worksheet, lngRow As Long, arr As Variant
- Dim strTemp As String, strPat As String
- Set sh = Sheets("Sheet2")
- lngRow = sh.Range("A" & Rows.Count).End(xlUp).Row
- arr = sh.Range("A2:A" & lngRow)
-
- For lngRow = LBound(arr) To UBound(arr)
- strTemp = Trim(CStr(arr(lngRow, 1)))
- If strTemp <> "" Then strPat = strPat & "|" & strTemp
- Next
- GetPat = Mid(strPat, 2)
- Set sh = Nothing
- End Function
复制代码 |
|