event if cell is changed - but only if the value is different from original value

tomleitch

Board Regular
Joined
Jan 3, 2012
Messages
189
Hi all,

I have some vba running on my sheet that colour fills a cell if it is changed.

Current code is this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A5:F2000")) Is Nothing Then Target.Interior.Color = rgbBeige
End Sub


However - I am pasting values into cells with another macro - and I only want the cells to be colour filled if the pasted value is different from the value that was in the cells in the first place.


Thanks
Tom
 
Last edited:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi,
To do what you want you will need a place holder to store the cell values to make the comparison with.
One possible way to achieve this would be to use the Range.ID property

Example
Code:
 Private Sub Worksheet_Change(ByVal Target As Range)    
If Not Intersect(Target, Range("A5:F2000")) Is Nothing Then
        With Target
        If .ID <> .Value Then
            .Interior.Color = rgbBeige
            .ID = .Value
        Else
            .Interior.Color = xlNone
        End If
        End With
    End If
End Sub

However, Range.ID does not (I think) retain its value when workbook is saved & closed so you may need to initialize them when workbook is opened

Example
Code:
 Private Sub Workbook_Open()    Dim rng As Range, cell As Range
    Set rng = Worksheets("Sheet1").Range("A5:F2000")
    
    For Each cell In rng.Cells
        cell.ID = cell.Value
    Next cell
End Sub

This is just an idea & you will need to adapt solution to meet specific project need as required

Others here may have alternative solutions.

Dave
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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