使用具有六个条件的图标集的条件格式
Using Conditional Formatting with Icon Sets with six conditions
我正在使用条件格式,我已经使用条件格式玩了几天,但我无法得到我正在寻找的响应。
我想要根据输入的标记在单元格中显示一个彩色圆圈。但问题是我有六个条件,但 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
你可以用 VBA 来完成。
Setup,画一个椭圆形并向下拖动单元格以复制它。完成后,您可以输入值或公式。
一旦你运行代码形状就会改变颜色。
代码
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
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
每当您更改作品中单元格的值时,这将 运行sheet。因为我很懒惰(并且在编码方面相当平庸)所以它只会在您一次更新单个单元格时起作用,并且它在整个工作中 运行ning sheet。但它会给你一个工作的起点。
如果您受限于图标集的条件格式规则:
如果您不需要圆圈,您的 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
注:
- VBA代码应该用小数据
- 它可以生成大量形状,这会使所有其他操作变慢
大约 1,000 行和 20 列的测试:总圆 19,250;持续时间:47.921875 秒
编辑:对子 setIcon()
进行了 2 次更新
- 自清洁
如果单元格不包含错误,它只处理数值
- 它排除了带有文本、空单元格或日期的单元格
- 感谢@EEM的建议
我正在使用条件格式,我已经使用条件格式玩了几天,但我无法得到我正在寻找的响应。
我想要根据输入的标记在单元格中显示一个彩色圆圈。但问题是我有六个条件,但 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
你可以用 VBA 来完成。
Setup,画一个椭圆形并向下拖动单元格以复制它。完成后,您可以输入值或公式。
一旦你运行代码形状就会改变颜色。
代码
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
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
每当您更改作品中单元格的值时,这将 运行sheet。因为我很懒惰(并且在编码方面相当平庸)所以它只会在您一次更新单个单元格时起作用,并且它在整个工作中 运行ning sheet。但它会给你一个工作的起点。
如果您受限于图标集的条件格式规则:
如果您不需要圆圈,您的 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
注:
- VBA代码应该用小数据
- 它可以生成大量形状,这会使所有其他操作变慢
大约 1,000 行和 20 列的测试:总圆 19,250;持续时间:47.921875 秒
编辑:对子 setIcon()
- 自清洁
如果单元格不包含错误,它只处理数值
- 它排除了带有文本、空单元格或日期的单元格
- 感谢@EEM的建议