繁体   English   中英

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

[英]Excel - Change Cell Color based on increase or decrease of previous value

您好,谢谢您的宝贵时间,

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

B1 = A1 * 1.15

由于COLUMN A的值每周都会波动,因此必须手动输入:我想知道COLUMN B是增加还是减少-我想指出颜色是增加还是减少。

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

如果我正确理解了您的问题,那么如果A列中的数字的值比先前输入的数字增加或减少,则您想在输入新数字时操纵B列的颜色。 ?

我刚刚在这里发布了如何确定单元格的先前值的方法: 在更改之前检测单元格中的

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM