Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2476|回复: 6

VBA 动态条件格式

[复制链接]
发表于 2017-5-28 09:27 | 显示全部楼层 |阅读模式
在工作表中,每当单元格内输入一个值,当超过达到或超过5个时,工作表中所有与本单元格(刚输入的)相同的单元格全部充填黄色。
用VBA代码如何编写?
我的代码总是不能运行,错误就在这里:
   Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=" & Target.Value & ""


下面是我的代码:
Private Sub Worksheet_Change(ByVal Target As Range)
   Application.ScreenUpdating = False
   Cells.Select  '去掉充填色
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Cells.Select    '清除条件格式
    Cells.FormatConditions.Delete

   warng = 5
   Set qy = Worksheets("Sheet1").Cells  '设置区域
    If Target.Value = "" Then GoTo 100   '空值略过
    mon = WorksheetFunction.CountIf(qy, Target)   '非空计数
    If mon >= warng Then    '与标准值对照,超过进行处理:
       Target.ClearComments   '清除原有标注

       '设置标注内容
       Text = "注意:" & Chr(10) & _
                Target.Address(0, 0) & "=" & Target.Value & "  出现  " & mon & "  次!"
       Target.AddComment   '添加标注
       Target.Comment.Text Text:=Text   '写入内容
       '对重复值区域进行充填底色,方便查找:

    Cells.Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
        Formula1:="=" & Target.Value & ""
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With

    End If
100:
    Cells(Target.Row + 1, Target.Column).Select
    Application.ScreenUpdating = True
End Sub

 楼主| 发表于 2017-5-28 09:56 | 显示全部楼层
我自己解决了:
Private Sub Worksheet_Change(ByVal Target As Range)
   Application.ScreenUpdating = False
   Cells.Select  '去掉充填色
    With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Cells.Select    '清除条件格式
    Cells.FormatConditions.Delete

   warng = 5
   Set qy = Worksheets("Sheet1").Cells  '设置区域
    If Target.Value = "" Then GoTo 100   '空值略过
    a = Target.Value
    mon = WorksheetFunction.CountIf(qy, Target)   '非空计数
    If mon >= warng Then    '与标准值对照,超过进行处理:
    Cells.Select
    Cells.ClearComments    '清除原有标注
      
       '设置标注内容
       Text = "注意:" & Chr(10) & _
                Target.Address(0, 0) & "=" & Target.Value & "  出现  " & mon & "  次!"
       Target.AddComment   '添加标注
       Target.Comment.Text Text:=Text   '写入内容
      
       '对重复值区域进行充填底色,方便查找:
        Cells.Select
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
            Formula1:="=" & """" & a & """"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .Color = 65535
            .TintAndShade = 0
        End With
End If
100:
    Cells(Target.Row + 1, Target.Column).Select
    Application.ScreenUpdating = True
End Sub

回复

使用道具 举报

 楼主| 发表于 2017-5-28 10:05 | 显示全部楼层
另一个问题:
字符可以正常工作,数值不行,大神们,怎么处理?
回复

使用道具 举报

发表于 2017-5-28 17:29 | 显示全部楼层
雄鹰 发表于 2017-5-28 10:05
另一个问题:
字符可以正常工作,数值不行,大神们,怎么处理?

可以投机使用Mid函数。比如b是数值,c就把它转化成了字符。
  1. Dim b, c As String
  2. b = 3
  3. c = Mid(b, 1)
复制代码



回复

使用道具 举报

 楼主| 发表于 2017-5-30 20:04 | 显示全部楼层
france723 发表于 2017-5-28 17:29
可以投机使用Mid函数。比如b是数值,c就把它转化成了字符。

谢谢,但是这不是解决问题的好办法,如何用VBA解决数字条件格式?
回复

使用道具 举报

发表于 2017-5-30 20:08 | 显示全部楼层
雄鹰 发表于 2017-5-30 20:04
谢谢,但是这不是解决问题的好办法,如何用VBA解决数字条件格式?

CStr函数进行转换
回复

使用道具 举报

 楼主| 发表于 2017-6-3 17:43 | 显示全部楼层
数值、字符串的可以了,但是日期型的还是不能充填颜色:
Private Sub Worksheet_Change(ByVal Target As Range)
   On Error GoTo 100  '遇错略过
   Application.ScreenUpdating = False   '关闭屏幕更新,加快响应
   warng = 5   '设置警报线
   Set qy = Worksheets("Sheet1").Cells  '设置区域
   If Target.Value = "" Then GoTo 100   '空值略过
   
   a = Target.Value + 0  '对输入参数进行类型判断区分
   If WorksheetFunction.IsNumber(a) Then
        mon = WorksheetFunction.CountIf(qy, Target.Value)    '数值型计数
   Else
        mon = WorksheetFunction.CountIf(qy, a)    '字符型计数
   End If
   
   If mon >= warng Then    '与警报线标准值对照,超过进行处理:
       Cells.Select  '去掉充填色
       With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
       End With
       Cells.FormatConditions.Delete    '清除条件格式
       Cells.ClearComments    '清除原有标注
            
      '设置标注内容
       Text = "注意:" & Chr(10) & _
                     Target.Address(0, 0) & "=" & Target.Value & "  出现  " & mon & "  次!"
       Target.AddComment   '添加标注
       Target.Comment.Text Text:=Text   '写入内容
            
      '对重复值区域进行充填底色,方便查找:
       If WorksheetFunction.IsNumber(a) Then
             Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
                 Formula1:="=" & a
             Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
             With Selection.FormatConditions(1).Interior
                 .Color = 65535
             End With
        Else
             Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, _
                 Formula1:="=" & """" & a & """"
             Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
             With Selection.FormatConditions(1).Interior
                 .Color = 65535
             End With
        End If
         
   End If
100:
    Cells(Target.Row + 1, Target.Column).Select
    Application.ScreenUpdating = True
End Sub
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-8 09:33 , Processed in 0.461533 second(s), 8 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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