简体   繁体   中英

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

Hi and thanks in advance for your time,

In COLUMN B I have a currency value based on a currency value in COLUMN A :

B1 = A1*1.15

Since the values of COLUMN A : fluctuate weekly and have to be manually inputted: I would like to know if there is an increase or decrease in COLUMN B - I'd like to indicate if there was an increase or decrease with a color.

The issue that I'm running into is that excel doesn't seem to like to reference the same cell's value.

If I understand your question properly, you want to manipulate the colour of column B when you enter a new number if the number in column A has increased or decreased in value from the previously entered number, the formula isn't anything to do with it?

I just posted on how to determine the previous value of a cell here: Detecting what value was in a cell prior to a change

You could apply that like this at the worksheet level:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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