使用带有六个条件的图标集的条件格式

时间:2015-09-12 08:39:09

标签: excel excel-vba excel-formula conditional-formatting vba

我使用条件格式,我已经玩了几天的条件格式化但我无法得到我正在寻找的响应。

我希望根据输入的标记在单元格中显示彩色圆圈。但问题是我有六个条件,但我认为Excel只支持五个。这可能吗?

0-20  red color circle
21-39 green color circle
40-54 blue color circle
55-64 yellow color circle
65-79 orange color circle
80-100 pink color circle

Example - icon style base conditional formatting

3 个答案:

答案 0 :(得分:3)

如果您受限于使用图标集的条件格式规则:

  • 如果您不必拥有圈子,可以轻松设置6条规则,如下图所示

  • 如果CF规则中需要4个以上的彩色圆圈:Create Your Own Excel Icon Set

如果你可以使用VBA,下面的代码将创建类似于原生CF圈的风格化圈子

  • 打开VBA: Alt + F11
  • 创建新模块:菜单项插入> 模块并粘贴代码
  • 点击第一个子testIcons()内的任意位置,然后按 F5 运行
Option Explicit

Public Sub testIcons()
   Application.ScreenUpdating = False
   setIcon Sheet1.UsedRange
   Application.ScreenUpdating = True
End Sub

Public Sub setIcon(ByRef rng As Range)
   Dim cel As Range, sh As Shape, adr As String

   For Each sh In rng.Parent.Shapes
      If InStrB(sh.Name, "$") > 0 Then sh.Delete
   Next: DoEvents
   For Each cel In rng
      If Not IsError(cel.Value2) Then
         If Val(cel.Value2) > 0 And Not IsDate(cel) Then
           adr = cel.Address
           Set sh = Sheet1.Shapes.AddShape(msoShapeOval, cel.Left + 5, cel.Top + 2, 10, 10)
           sh.ShapeStyle = msoShapeStylePreset38: sh.Name = adr
           sh.Fill.ForeColor.RGB = getCelColor(Val(cel.Value2))
           sh.Fill.Solid
         End If
      End If
   Next
End Sub

Public Function getCelColor(ByRef celVal As Long) As Long
   Select Case True
      Case celVal < 21:    getCelColor = RGB(222, 0, 0):    Exit Function
      Case celVal < 40:    getCelColor = RGB(0, 111, 0):    Exit Function
      Case celVal < 55:    getCelColor = RGB(0, 0, 255):    Exit Function
      Case celVal < 64:    getCelColor = RGB(200, 200, 0):  Exit Function
      Case celVal < 80:    getCelColor = RGB(200, 100, 0):  Exit Function
      Case celVal <= 100:  getCelColor = RGB(200, 0, 200):  Exit Function
   End Select
End Function

enter image description here

注意

  • VBA代码应与小数据一起使用
  • 它可以生成大量的形状,这将使所有其他操作变慢

大约1,000行和20列的测试:总圈数 19,250 ;持续时间: 47.921875秒

修改:对子setIcon()进行了2次更新

  1. 自清洁
  2. 如果单元格不包含错误,则仅处理数值

    • 它排除包含文本,空单元格或日期的单元格
    • 感谢@EEM
    • 的建议

答案 1 :(得分:2)

您可以使用VBA。

“设置”,绘制椭圆形状并向下拖动单元格以复制它。完成后,您可以输入值或公式。

enter image description here

运行代码后,形状会改变颜色。

enter image description here

守则

Sub Button1_Click()
    Dim sh As Shape
    Dim I As Integer
    Dim r As String, rng As Range

    I = 1
    For Each sh In ActiveSheet.Shapes

        If sh.Name = "Oval " & I Then

            r = sh.TopLeftCell.Address    'find the range of the button clicked.

            Set rng = Range(r)

            Select Case rng

            Case Is < 21
                ActiveSheet.Shapes(sh.Name).Fill.ForeColor.RGB = 255

            Case Is < 40
                ActiveSheet.Shapes(sh.Name).Fill.ForeColor.RGB = 5287936

            Case Is < 55
                ActiveSheet.Shapes(sh.Name).Fill.ForeColor.RGB = 12611584

            Case Is < 65
                ActiveSheet.Shapes(sh.Name).Fill.ForeColor.RGB = 65535

            Case Is < 80
                ActiveSheet.Shapes(sh.Name).Fill.ForeColor.RGB = RGB(255, 153, 51)

            Case Is < 101
                ActiveSheet.Shapes(sh.Name).Fill.ForeColor.RGB = RGB(255, 153, 204)

            Case Else
            End Select

            I = I + 1

        End If

    Next


End Sub

Sample Workbook

答案 2 :(得分:1)

VBA是我知道这样做的唯一方法。如果你可以应对整个细胞的颜色,那么这可能适合你:

Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo Finish
Application.EnableEvents = False

If Target.Count > 1 Then GoTo Finish

If Target.Value = "" Then
    Target.Interior.Color = -4142 ' no colour
    GoTo Finish
ElseIf Target.Value < 21 Then
    Target.Interior.ColorIndex = 3 'red
    GoTo Finish
ElseIf Target.Value < 40 Then
    Target.Interior.ColorIndex = 10 'green
    GoTo Finish
ElseIf Target.Value < 55 Then
    Target.Interior.ColorIndex = 23 'blue
GoTo Finish
ElseIf Target.Value < 65 Then
    Target.Interior.ColorIndex = 6 'yellow
    GoTo Finish
ElseIf Target.Value < 80 Then
    Target.Interior.ColorIndex = 45 'orange
    GoTo Finish
ElseIf Target.Value < 101 Then
    Target.Interior.ColorIndex = 7 ' pink
Else
    Target.ColorIndex = -4142
End If


Finish: Application.EnableEvents = True

End Sub

当您更改工作表中单元格的值时,将运行此选项。因为我很懒(而且在编码时非常平庸)它只会在你一次更新一个单元格时运行,并且它在整个工作表上运行。但它会为你提供一个起点。