基于颜色的 Excel 条件格式化数据条

12

我找不到一种方法来根据数值改变Excel数据条的颜色。当前的格式选项只允许基于正/负数值使用不同的颜色。我当前使用的是Excel 2010。

我想让数据条的颜色在数值在0-0.3之间时显示为“红色”,在0.3-0.6之间时显示为“黄色”,在>0.6之间时显示为“绿色”。

如果有任何信息可以分享,我会非常感激。

谢谢,

TB


我找到了一个类似的网站,尽管我无法让VBA代码正常工作。也许有更多经验的人可以看一下? - TheBlake
https://digimac.wordpress.com/2014/06/29/multicoloured-data-bars-in-excel/ - TheBlake
你需要做的第一件事是给你的数据添加一些红色数据条,然后再添加一些绿色数据条。默认情况下,Excel会显示最后应用的设置,所以数据条将是绿色的。如果你启动VB编辑器(Alt + F11),并在立即窗口(Ctrl + G)中键入以下内容: selection.FormatConditions(1).formula = "=if(c3>59, true, false)" - TheBlake
当我尝试这个解决方案时,我得到了以下错误:_编译错误:预期表达式_。 - TheBlake
我是Blake发现的插件的作者。我会尽力抽出时间进行一些调试。它在我的系统上可以正常工作,所以我只需要找出是什么原因导致大多数其他人无法使用它。 - DiGiMac
4个回答

16

Data bars只支持每组数据一个颜色。这样做的想法是,数据条的长度可以向您指示高、中或低。

可以使用颜色刻度来实现条件颜色。

您所描述的听起来像两者的结合,但在Excel中不存在,我也看不到简单的方法来hack它。

您可以使用一种称为“单元格内图表”的方式,在火花线出现之前很受欢迎。使用公式重复字符(在截图中是使用Marlett字体格式化的字符g),然后使用条件格式更改字体颜色。

enter image description here

为了获得更好的“条形”感觉,请使用带有常规字体的Unicode字符2588。

enter image description here

编辑:并非每个Unicode字符都在每个字体中都有表示。在这种情况下,Unicode 2588在Arial字体中显示正常,但在Excel的默认Calibri字体中不能正确显示。选择适当的字体。插入>符号对话框将有助于找到合适的字符。

enter image description here


谢谢这个提示!虽然不完全是我要找的,但已经有了90%的进展。一个快速的问题,你确定Unicode是2588吗?当我输入这个Unicode时,它显示为小方框,类似于你的第一个示例。我不知道该如何使它看起来像你第二张图片中的条形图。 - TheBlake
抱歉,只是澄清一下,Unicode 2558 显示为小的“块”,而 Unicode 2588 实际上显示为一个小的 L(但 L 的两条线长度相等)。 - TheBlake
抱歉,我刚刚意识到使用任何字体都无法显示“块”字符的2588 Unicode。但是在Arial字体中可以显示。因此我编辑了我的帖子。 - teylyn
不错的技术,但如果您希望颜色/完成独立,例如您想显示任务完成加RAG状态,则无法帮助。 - adolf garlic
@adolfgarlic 嗯,那不是问题,对吧?但这仍然可以完成。使用一个 CF 规则,查看另一个单元格的值以确定颜色。 - teylyn

1

我在数据条旁边的单元格中设置了条件格式,根据目标单元格中的值更改颜色(绿色、黄色、红色、橙色)。然后,我通过下面的VBA循环来根据相邻单元格中的条件格式更新数据条颜色。

Dim intCount As Integer
Dim db As DataBar

On Error Resume Next
For intCount = 9 To 43 'rows with data bars to be updated
    Worksheets("Worksheet Name").Cells(intCount, 10).FormatConditions(1).BarColor.Color = Worksheets("Worksheet Name").Cells(intCount, 11).DisplayFormat.Interior.Color
Next intCount

0
在您的情况下,突出显示单元格将更加合适,因为我们无法使用多种颜色形成数据条
条件格式化>管理规则...>新建规则
在选择规则类型下,选择“使用公式确定要格式化的单元格”,并在那里设置您的规则
enter image description here

0

不必为一系列单元格创建条件格式,我使用基于VBA的下面两个子程序逐个对每个单元格进行有条件地格式化。结果在代码下面的链接中显示。希望这可以帮到你。

' The purpose of this sub is to add a data bar to an individual cell
' The value in the cell is expected to be decimal numbers between -1 and 1
' If the value is greater than or equal to -0.1 and less than or equal to 0.1, then display green bars
' If the value is less than -0.1 and greater than -.2, OR greater than 0.1 and less than 0.2 then yellow bars
' All other scenarios display red bars
Sub Add_Data_Bar(rngCell As Range, dblValue As Double)

' Clears existing conditional formatting from the cell
' Adds a new data bar to the cell
With rngCell.FormatConditions
    .Delete
    .AddDatabar
End With

    ' Creates a databar object for the databar that has been added to the cell
    Dim dbar As Databar
    Set dbar = rngCell.FormatConditions(rngCell.FormatConditions.Count)

        ' Sets the databar fill type to display as gradient
        dbar.BarFillType = xlDataBarFillGradient

        ' Sets the databar border style
        dbar.BarBorder.Type = xlDataBarBorderSolid

        ' Sets the databar axis position
        dbar.AxisPosition = xlDataBarAxisMidpoint

        ' Sets the minimum limit of the data bar to -1
        With dbar.MinPoint
            .Modify newtype:=xlConditionValueNumber, newvalue:=-1
        End With

        ' Sets the maximum limit of the data bar to +1
        With dbar.MaxPoint
            .Modify newtype:=xlConditionValueNumber, newvalue:=1
        End With

            ' Sets the color based on what value has been passed to the sub
            ' Green
            If dblValue <= 0.1 And dblValue >= -0.1 Then

                With dbar
                    .BarColor.Color = RGB(99, 195, 132) ' Green
                    .BarBorder.Color.Color = RGB(99, 195, 132)
                End With

            ' Yellow
            ElseIf (dblValue > 0.1 And dblValue <= 0.2) Or (dblValue < -0.1 And dblValue >= -0.2) Then

                With dbar
                    .BarColor.Color = RGB(255, 182, 40) ' Yellow
                    .BarBorder.Color.Color = RGB(255, 182, 40)
                End With

            ' Red
            Else

                With dbar
                    .BarColor.Color = RGB(255, 0, 0) ' Red
                    .BarBorder.Color.Color = RGB(255, 0, 0)
                End With

            End If

End Sub


' Applies the databar formatting to each cell in a range
‘ Call this on the Worksheet_Change event so that the formatting updates when data is refreshed
Sub Loop_Through_Range()

' Range to be looped through
Dim rng As Range
Set rng = Sheet1.Range("A2:A22")

' Range for For Loop
Dim cell As Range

    ' Loops through each cell in your range
    For Each cell In rng.Cells
        Call Add_Data_Bar(cell, cell.Value)
    Next

End Sub

工作表视图


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接