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

时间:2015-06-04 01:11:39

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

我找不到基于值更改Excel数据栏颜色的方法。当前格式化选项仅允许基于正/负值的不同颜色。我目前正在使用Excel 2010。

我想将数据条的颜色显示为“红色”,如果值介于0-0.3之间,“黄色”如果值介于0.3-0.6之间,则“绿色”如果值介于两者之间> 0.6

非常感谢人们可以分享的任何信息。

谢谢,

TB

5 个答案:

答案 0 :(得分:10)

数据栏每组只支持一种颜色。我们的想法是数据栏的长度为您提供高,中或低的指示。

使用色标可以实现条件色彩。

你所描述的内容听起来像是两者的结合,但Excel中并不存在,我也没有看到一种简单的方法来破解它。

你可以使用一种内嵌式"图表"在迷你电影出现之前很受欢迎。使用公式重复一个字符(在屏幕截图中它是用Marlett字体格式化的字符g),然后使用条件格式来更改字体颜色。

enter image description here

更好的"酒吧"感觉,使用带有常规字体的unicode字符2588。

enter image description here

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

enter image description here

答案 1 :(得分:1)

本文介绍了完成这项工作的技巧:

http://www.excel-user.com/2012/05/conditional-formatting-bar-chart.html

enter image description here

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我在数据栏旁边的单元格中设置条件格式,根据目标单元格中​​的值(绿色,黄色,红色,橙色)更改颜色。然后,我遍历下面的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

答案 4 :(得分: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

Worksheet View