Excel VBA:Worksheet_change不检测重新计算

时间:2016-11-15 19:38:59

标签: excel vba excel-vba

我有这个VBA功能,适用于手动更改单元格,但它对= AVERAGE()函数所在的列有效。我想要做的是改变平均颜色的字体颜色。如果average大于0.6,则font colorIndex为3,如果平均列值小于或等于0.6,则将font colorIndex设置为10.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Range
    Set c = Range(Target.Dependents.Address)
    For Each c In Target.Cells
        If Not Intersect(c, Range("X:AI")) Is Nothing Then
            If c > Range("H" & c.Row).Value Or c < Range("G" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("H" & c.Row).Value And c >= Range("G" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If

...

        ElseIf Not Intersect(c, Range("AX:AX")) Is Nothing Then
            If c > 0.6 Then
                c.Font.ColorIndex = 3
            ElseIf c <= 0.6 Then
                c.Font.ColorIndex = 10
            End If
        End If
    Next c
End Sub

1 个答案:

答案 0 :(得分:0)

这是我找到的为我工作的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Range
    For Each c In Target.Cells
        If Not Intersect(c, Range("X:AI")) Is Nothing Then
            If c > Range("H" & c.Row).Value Or c < Range("G" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("H" & c.Row).Value And c >= Range("G" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
            Range("AK" & c.Row).Value = "=AVERAGE(X" & c.Row & ":AI" & c.Row & ")"

        ElseIf Not Intersect(c, Range("AL:AM")) Is Nothing Then
            If c > Range("K" & c.Row).Value Or c < Range("J" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("K" & c.Row).Value And c >= Range("J" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
        ElseIf Not Intersect(c, Range("AN:AP")) Is Nothing Then
            If c > Range("N" & c.Row).Value Or c < Range("M" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("N" & c.Row).Value And c >= Range("M" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
            Range("AQ" & c.Row).Value = "=AVERAGE(AN" & c.Row & ":AP" & c.Row & ")"

        ElseIf Not Intersect(c, Range("AR:AT")) Is Nothing Then
            If c > Range("Q" & c.Row).Value Or c < Range("P" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("Q" & c.Row).Value And c >= Range("P" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
            Range("AU" & c.Row).Value = "=AVERAGE(AR" & c.Row & ":AT" & c.Row & ")"
        ElseIf Not Intersect(c, Range("AV:AX")) Is Nothing Then
            If c > Range("T" & c.Row).Value Or c < Range("S" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("T" & c.Row).Value And c >= Range("S" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
            Range("AY" & c.Row).Value = "=AVERAGE(AV" & c.Row & ":AX" & c.Row & ")"
        ElseIf Not Intersect(c, Range("BE:BG")) Is Nothing Then
            If c > Range("T" & c.Row).Value Or c < Range("S" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("T" & c.Row).Value And c >= Range("S" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
        ElseIf Not Intersect(c, Range("AK:AK")) Is Nothing Then
            If c > Range("H" & c.Row).Value Or c < Range("G" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("H" & c.Row).Value And c >= Range("G" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
        ElseIf Not Intersect(c, Range("AU:AU")) Is Nothing Then
            If c > Range("Q" & c.Row).Value Or c < Range("P" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("Q" & c.Row).Value And c >= Range("P" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
        ElseIf Not Intersect(c, Range("AY:AY")) Is Nothing Then
            If c > Range("T" & c.Row).Value Or c < Range("S" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("T" & c.Row).Value And c >= Range("S" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
        End If
    Next c
End Sub

此行手动更新平均单元格,然后Worksheet_Change完成工作。

Range("AK" & c.Row).Value = "=AVERAGE(X" & c.Row & ":AI" & c.Row & ")"