Color Cell When Cell Value is actually Changed

imakshar

New Member
Joined
Mar 6, 2012
Messages
1
I am pretty new to Excel Macros & I have following code in my current spreadsheet as macro to highlight a cell when cell value changes for Column C. The way it works is
****************************************************
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 1 Then Exit Sub 'Do not change Row 1
If Target.Column <> 3 Then Exit Sub 'only allow changes to Col C
Range(Target.Address).Activate
If ActiveCell <> "" Then
ActiveCell.Interior.ColorIndex = 27 'change to colour of your choice
Else
ActiveCell.Interior.ColorIndex = -4142
End If
End Sub
****************************************************
1. when any cell in Column C is selected (i mean double click) than it changes the color assuming that active cell value changed.
2. when any cell value is edited, it changes the color.
3. if i just select a cell (no double click) and hit delete button, cell is cleared but not recognized as cell change so no color in that cell.

What i am trying to achieve is when a cell value is edited (and only edited .. it includes delete, backspace, insert etc) than it changes the color. Just double click should not change the color as it has not edited yet.

Can someone please help me with right code ?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi imakshar and welcome to the forum.

Try this, copy the below code to the Worksheet code window
Code:
[FONT="Consolas"][SIZE="2"][COLOR="Navy"]Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row = 1 And Target.Column <> 3 Then Exit Sub [COLOR="Green"]'only allow changes to Col C[/COLOR]
    SetCellColor Target
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    SaveOldValue Target
End Sub[/COLOR][/SIZE][/FONT]
Insert new Module and copy the below code to its code window
Code:
[FONT="Consolas"][SIZE="2"][COLOR="Navy"]Option Explicit

Private vCurrentValue As Variant

Sub SaveOldValue(Target As Range)
    vCurrentValue = Target.Value
End Sub

Sub SetCellColor(Target As Range)

    If Target = vCurrentValue Then Exit Sub
    If Target <> "" Then
        Target.Interior.ColorIndex = 27 [COLOR="Green"]'change to colour of your choice[/COLOR]
    Else
        Target.Interior.ColorIndex = -4142
    End If

End Sub[/COLOR][/SIZE][/FONT]
 
Upvote 0
Just discovered a typo mistake

please replace Worksheet_Change with this one

Code:
[FONT="Consolas"][SIZE="2"][COLOR="Navy"]Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Row = 1 Or Target.Column <> 3 Then Exit Sub [COLOR="Green"]'only allow changes to Col C[/COLOR]
    SetCellColor Target
End Sub[/COLOR][/SIZE][/FONT]
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top