簡體   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