0

我有一个基于以下查询的数据表表单,它是几个数据集的联合,这些数据集已被旋转以按日期提供列:

+-------------+------------------+----------+----------+-----------+
| statusType  |    valueType     | 8/1/2013 | 9/1/2013 | 10/1/2013 |
+-------------+------------------+----------+----------+-----------+
| design      | actual           |        3 |        6 |         7 |
| design      | target           |        4 |        5 |         4 |
| design      | cumulativeActual |       60 |       66 |        67 |
| design      | cumulativeTarget |       50 |       55 |        54 |
| development | actual           |       10 |       12 |         2 |
| development | target           |       10 |        8 |         8 |
| development | cumulativeActual |       30 |       42 |        44 |
| development | cumulativeTarget |       40 |       48 |        56 |
+-------------+------------------+----------+----------+-----------+  ...

我想为每个日期列中的“实际”值设置条件格式,以便为每个值设置颜色,如下所示:

  • 实际 >= 目标:绿色
  • 实际 = 目标的 51-99%:蓝色
  • 实际 < 50% 目标:红色

因此,例如,8/1 的“设计实际”值将是蓝色,而 9/1 和 10/1 的“开发实际”值将分别为绿色和红色。

我可以遍历设置其条件格式的日期控件,如下所示:

Private Sub Form_Load()

Dim ctrl As Control
Dim tb As TextBox


For Each ctrl In Me.Controls
    If IsDate(ctrl.Name) Then
        Set tb = ctrl
        tb.FormatConditions.Add 'not sure what to put here
    End If
Next

End Sub

我不明白我应该在FormatConditions.Add这里提出什么论据;对于给定的statusType// actualdate 值,我将如何获取相应的statusType// targetdate 值以便比较它们以设置格式?

请注意,此表单是只读的,因此也许它不必是有条件的——也许我可以在加载表单时根据上述规则为值分配静态颜色。

附加说明:基本上,我正在尝试做的是根据它们与另一行中同一列中的值的比较方式,一行中的颜色值。例如,对于“2013 年 8 月 1 日”列,比较“设计|实际”值 3 和“设计|目标”值 4 以确定“设计|实际”值应该是什么颜色。

4

1 回答 1

0

I guess I don't quite understand what you're trying to do.

If you need different conditional formatting configured for each row individually, I don't think that's going to work. If you cannot setup your conditional formatting in Design View then you probably won't get it to work using code either.

In case you still want some code, here's how you do conditional formatting in code using Expressions:

Private Sub Form_Load()

    Dim ctrl As Control
    Dim tb As TextBox
    Dim fc as FormatCondition

    For Each ctrl In Me.Controls
        If IsDate(ctrl.Name) Then
            Set tb = ctrl
            Set fc = tb.FormatConditions.Add(acExpression, , "Me![FieldName] = ""Value""")
            fc.BackColor = 13611711
        End If
    Next

End Sub
于 2013-08-13T20:00:43.220 回答