Excel - 根据之前值的增加或减少更改单元格颜色

时间:2015-03-03 21:32:30

标签: excel

您好,并提前感谢您的时间,

COLUMN B 中,我的货币值基于 COLUMN A 中的货币值:

B1 = A1 * 1.15

由于 COLUMN A 的值:每周波​​动,必须手动输入:我想知道 COLUMN B 是否增加或减少 - 我我想表明是否有颜色增加或减少。

我遇到的问题是excel似乎不想引用同一个单元格的值。

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你想在输入一个新数字时操纵B栏的颜色,如果A栏中的数字的值增加或减少了先前输入的数字,那么公式就不是做到了吗?

我刚刚发布了如何在此处确定单元格的上一个值:Detecting what value was in a cell prior to a change

您可以在工作表级别应用这样的内容:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim OldValue As Variant, NewValue As Variant
'If in a column other than A then end
If Not Target.Column = 1 Then End
'If the entered value is not numeric then clear shade and end
If Not IsNumeric(Target.Value) Then
    Target.Offset(0, 1).Interior.Pattern = xlNone
    End
End If
'Populate NewValue with value of target
NewValue = Target.Value
'Turn the events off
Application.EnableEvents = False
'Undo the change
Application.Undo
'Populate OldValue with the undone value
OldValue = Target.Value
'Make the target the NewValue once again
Target.Value = NewValue
'Do a comparison on NewValue and OldValue
If NewValue > OldValue Then
    'Shade Green
    With Target.Offset(0, 1).Interior
        .Pattern = xlSolid
        .Color = 5287936
    End With
ElseIf NewValue < OldValue Then
    'Shade Red
    With Target.Offset(0, 1).Interior
        .Pattern = xlSolid
        .Color = 255
    End With
Else
    'Clear shade
    Target.Offset(0, 1).Interior.Pattern = xlNone
End If
Application.EnableEvents = True
End Sub