Excel 2007/2010 基于公式的色阶条件格式

发布于 2024-11-02 16:49:16 字数 477 浏览 5 评论 0 原文

假设我有以下表格结构 (A1:C3)

     A   B   C
1    H1  H2  H3
2    1   1   3
3    4   5   4

如何应用具有以下条件的条件格式:

  • 仅标题单元格 (H1,H2,H3) 着色
  • 着色方案为 2 或 3 色阶
  • 值用于计算颜色的应该是 A2/A3、B2/B3、C2/C3(范围是 0-1)

注意:我不是在寻找 VBA 解决方案,因为我可以自己制作,但如果您有一些疯狂的 1 班轮请告诉:)
注意:我不想应用很多规则,例如 if x<0.3 redif 0.3 等。

有什么干净的解决方案吗?
可以根据公式的值应用 3 色标吗?

Suppose I have the following table structure (A1:C3)

     A   B   C
1    H1  H2  H3
2    1   1   3
3    4   5   4

How can I apply a conditional formatting with the following conditions:

  • Only the header cells (H1,H2,H3) are colored
  • The coloring scheme is 2 or 3-color-scale
  • The values used for computing the color should be A2/A3, B2/B3, C2/C3 (range is 0-1)

Note: I'm not looking for a VBA solution, as I can make my own, but if you have some crazy 1 liner please tell :)
Note: I don't want to apply a lot of rules like if x<0.3 red, if 0.3<x<0.6 orange etc.

Is there any clean solution for this?
Can a 3-color-scale be applied based on the value of a formula?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无声情话 2024-11-09 16:49:16

我认为您正在寻找一些有趣的东西,但不幸的是并不存在。我只看到两个选项。

您可以使用三个条件进行条件格式设置,如果您想使用默认单元格颜色作为三个条件之一,甚至可以只使用两个条件格式(例如,公式为 =(A2/A3)<0.3,公式为 =(A2/A3) <0.6,或任何您想要的阈值)。您可以在 Excel 中的一个标题单元格上手动应用此格式,然后将其复制到所有标题;或通过VBA应用它。

或者,您可以在 VBA 中为标题着色,然后在工作表上放置一个按钮来调用代码来更新颜色。

With Range("A1")
    For i = 1 To 3
        colorscale = .Cells(2, i).Value / .Cells(3, i).Value
        .Cells(1, i).Interior.Color = _
            RGB(colorscale * 255, colorscale * 255, _
            colorscale * 255) ' for example. Or pick your colors otherwise.
    Next i
End With

在我看来,第一个选项(条件格式)更适合,因为它会根据单元格值自动更新颜色。不过,我必须承认,我不太明白为什么你要寻找比这更“干净”的东西,因为它实际上是一个非常简单的解决方案。两个条件就构成“很多规则”吗?不是英语的任何延伸......

I think you're looking for something nifty that unfortunately doesn't exist. I just see the two options.

You can either do conditional formatting with three conditions, or even just two if you want to use the default cell color as one of the three (e.g. Formula Is =(A2/A3)<0.3, Formula Is =(A2/A3)<0.6, or whatever you want your threshold values to be). You can apply this fomatting manually on one header cell in Excel, and copy it across to all headers; or apply it through VBA.

Or you can color the headers in VBA, and place a button on your sheet that calls the code to update the colors.

With Range("A1")
    For i = 1 To 3
        colorscale = .Cells(2, i).Value / .Cells(3, i).Value
        .Cells(1, i).Interior.Color = _
            RGB(colorscale * 255, colorscale * 255, _
            colorscale * 255) ' for example. Or pick your colors otherwise.
    Next i
End With

Sounds to me like the first option (conditional formatting) is better suited, because it will update the colors automatically based on the cell values. I have to admit, though, I don't quite see why you're looking for something "cleaner" than this, because it's actually a really simple solution. Do two conditions constitute "a lot of rules"? Not by any stretch of the English language...

倒数 2024-11-09 16:49:16

以下是 Jean-François Corbett 代码的改编版,它从红-黄-绿渐变中获取颜色:

With Range("A1")
    For i = 1 To 3
        ColorScale = .Cells(2, i).Value / .Cells(3, i).Value
        If ColorScale < 0.5 Then
            red = 255
            green = ColorScale * 2 * 255
        Else
            red = (1 - ColorScale) * 2 * 255
            green = 255
        End If

        .Cells(1, i).Interior.Color = RGB(red, green, 0)
    Next i
End With

Here is an adaptation of Jean-François Corbett's code that takes the color from a red-yellow-green gradient:

With Range("A1")
    For i = 1 To 3
        ColorScale = .Cells(2, i).Value / .Cells(3, i).Value
        If ColorScale < 0.5 Then
            red = 255
            green = ColorScale * 2 * 255
        Else
            red = (1 - ColorScale) * 2 * 255
            green = 255
        End If

        .Cells(1, i).Interior.Color = RGB(red, green, 0)
    Next i
End With
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文