ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 3726|回复: 11

VBA判断单元格是否包含某些字符并改色

[复制链接]

TA的精华主题

TA的得分主题

发表于 2021-2-3 12:10 | 显示全部楼层 |阅读模式
请教各位老师,用VBA判断sheet1表的A列中的单元格,如果不包含sheet2中A列的一些关键字,就把sheet1表的A列的单元格标记红色
23.png

新建 Microsoft Excel 工作表 (2).zip

9.54 KB, 下载次数: 21

TA的精华主题

TA的得分主题

发表于 2021-2-3 12:40 | 显示全部楼层
Sub 按钮1_Click()
   
    arr = Sheets(2).[a1].CurrentRegion
    str1 = ""
    For j = 1 To UBound(arr)
        If Len(arr(j, 1)) > 0 Then str1 = str1 & "|" & arr(j, 1)
    Next j
    If Len(str1) = 0 Then
        MsgBox "表2没有匹配的字符"
        Exit Sub
    End If
    str1 = Mid(str1, 2)
    Set Rng = Nothing
    arr = Sheets(1).[a1].CurrentRegion
    Application.ScreenUpdating = False
    Sheets(1).Select
    With CreateObject("vbscript.regexp")
        .Global = True
        .Pattern = str1
        .ignorecase = True
        For j = 2 To UBound(arr)
            If Not .test(arr(j, 1)) Then
                If Rng Is Nothing Then
                    Set Rng = Cells(j, 1)
                Else
                    Set Rng = Union(Rng, Cells(j, 1))
                End If
            End If
        Next j
    End With
    [a1].CurrentRegion.Interior.ColorIndex = xlNone
    If Not Rng Is Nothing Then Rng.Interior.ColorIndex = 6
    Application.ScreenUpdating = True
End Sub

TA的精华主题

TA的得分主题

发表于 2021-2-3 12:40 | 显示全部楼层
附件内容供参考。。。。。。

新建 Microsoft Excel 工作表 (2).zip

17.99 KB, 下载次数: 65

TA的精华主题

TA的得分主题

发表于 2021-2-3 12:57 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
  1. Sub CheckHasKey()
  2.     Dim sh As Worksheet, lngRow As Long, arr As Variant
  3.     Dim objReg As Object, strTemp As String, strPat As String
  4.    
  5.     strPat = GetPat
  6.     If strPat = "" Then Exit Sub
  7.    
  8.     Set sh = Sheets("Sheet1")
  9.     lngRow = sh.Range("A" & Rows.Count).End(xlUp).Row
  10.     If lngRow < 2 Then Exit Sub
  11.    
  12.     sh.Range("A1:A" & lngRow).Interior.ColorIndex = 0
  13.     arr = sh.Range("A1:A" & lngRow)
  14.     Set objReg = CreateObject("VBScript.RegExp")
  15.     With objReg
  16.         .Global = True
  17.         .Pattern = strPat
  18.     End With
  19.    
  20.     For lngRow = LBound(arr) + 1 To UBound(arr)
  21.         strTemp = CStr(arr(lngRow, 1))
  22.         '不含,背景色标红
  23.         If Not objReg.test(strTemp) Then sh.Range("A" & lngRow).Interior.ColorIndex = 3
  24.     Next
  25.    
  26.     Set objReg = Nothing
  27.     Set sh = Nothing
  28. End Sub

  29. Function GetPat() As String
  30.     Dim sh As Worksheet, lngRow As Long, arr As Variant
  31.     Dim strTemp As String, strPat As String
  32.     Set sh = Sheets("Sheet2")
  33.     lngRow = sh.Range("A" & Rows.Count).End(xlUp).Row
  34.     arr = sh.Range("A2:A" & lngRow)
  35.    
  36.     For lngRow = LBound(arr) To UBound(arr)
  37.         strTemp = Trim(CStr(arr(lngRow, 1)))
  38.         If strTemp <> "" Then strPat = strPat & "|" & strTemp
  39.     Next
  40.     GetPat = Mid(strPat, 2)
  41.     Set sh = Nothing
  42. End Function
复制代码

TA的精华主题

TA的得分主题

发表于 2021-2-3 13:37 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
附件供参考

新建 Microsoft Excel 工作表.zip

19.09 KB, 下载次数: 49

TA的精华主题

TA的得分主题

发表于 2021-2-3 17:31 | 显示全部楼层
  1. Sub ss()
  2.    Dim i%, j%, arr, brr, m%
  3.    arr = Sheet1.Range("A1").CurrentRegion.Value
  4.    brr = Sheet2.Range("A1").CurrentRegion.Value
  5.    With Sheet1
  6.      .Range("A2:A" & UBound(arr)).Interior.ColorIndex = 0
  7.      For i = 2 To UBound(arr)
  8.         m = 0
  9.         For j = 1 To UBound(brr)
  10.            m = InStr(arr(i, 1), brr(j, 1))
  11.            If m > 0 Then Exit For
  12.         Next
  13.         If m = 0 Then .Range("A" & i).Interior.Color = vbRed
  14.      Next
  15.    End With
  16. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2021-2-3 17:54 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-3 18:35 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

TA的精华主题

TA的得分主题

 楼主| 发表于 2021-2-3 18:37 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2023-3-21 08:37 | 显示全部楼层

为什么我按你的代码测试就不行呢,其余2位老师提供的代码都可以,求解答!
QQ拼音截图未命名.jpg
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-9-29 18:27 , Processed in 0.037875 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表