【问题标题】:Excel Conditional Formatting Data Bars based on Color基于颜色的 Excel 条件格式数据条
【发布时间】:2015-08-18 10:43:56
【问题描述】:

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

如果值在 0-0.3 之间,我希望数据栏的颜色显示为“红色”,如果值在 0.3-0.6 之间,则显示为“黄色”,如果值在之间,则显示为“绿色” >0.6.

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

谢谢,

TB

【问题讨论】:

  • 我找到了一个实现类似功能的网站,尽管我无法让 VBA 代码正常工作。或许有更多经验的人可以看看?
  • 您要做的第一件事是在数据中添加一些红色数据条,然后再添加一些绿色数据条。默认情况下,Excel 会向您显示最后应用的集合,因此数据栏将为绿色。如果您随后启动 VB 编辑器 (Alt + F11) 并在即时窗口 (Ctrl+G) 中键入:selection.FormatConditions(1).formula = “=if(c3>59, true, false)”
  • 当我尝试这个解决方案时,我收到以下错误:编译错误:预期:表达式
  • 我是 Blake 发现的插件的作者。我会尽量腾出时间做一些调试。它适用于我的系统,所以我只需要弄清楚是什么阻止了大多数其他人。 ![enter image description here](i.stack.imgur.com/O3SXG.jpg)

标签: excel excel-2010 conditional-formatting vba


【解决方案1】:

数据条每组仅支持一种颜色。这个想法是数据条的长度可以指示高、中或低。

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

您所描述的内容听起来像是两者的结合,但这在 Excel 中不存在,而且我看不到破解它的简单方法。

您可以使用一种在迷你图出现之前流行的单元格内“图表”。使用公式重复一个字符(在屏幕截图中是使用 Marlett 字体格式化的字符 g),然后使用条件格式更改字体颜色。

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

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

【讨论】:

  • 感谢您的提示!虽然不完全是我正在寻找的东西,但它的 90% 在那里。一个简单的问题,你确定 unicode 是 2588 吗?当我输入这个 unicode 时,它​​会以小框的形式出现,类似于您的第一个示例。根据您的第二张图片,我不确定如何使它看起来像条形图
  • 抱歉,澄清一下,unicode 2558 以小“块”的形式出现,而 unicode 2588 实际上以小 L 出现(但 L 的两行长度相等)
  • 对不起,我刚刚意识到 2588 unicode 不显示任何字体的“块”字符。但它确实与 Arial。所以我编辑了我的帖子。
  • 不错的技术,但如果您希望颜色/完成是独立的,例如你想显示任务完成和 RAG 状态
  • @adolfgarlic 好吧,这不是问题,是吗?但它仍然可以完成。使用 CF 规则查看另一个单元格的值来确定颜色。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      我没有为一系列单元格创建条件格式,而是根据下面的两个子项使用 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

      【讨论】:

        猜你喜欢
        • 2018-11-25
        • 2016-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-20
        • 1970-01-01
        相关资源
        最近更新 更多