Change color of cell to Yellow once I copy it

Saher Naji

Board Regular
Joined
Dec 19, 2019
Messages
76
Office Version
  1. 2013
Platform
  1. Windows
Hello,

I'm trying to use this code to change the background color of a cell to yellow when I press Ctrl+C and has it stay yellow. This should be applied to every cell I copy.

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Application.CutCopyMode = xlCopy Then
        Target.Interior.Color = RGB(255, 255, 0) ' Yellow color
    Else
        Target.Interior.Pattern = xlNone ' Reset the cell's fill color
    End If
End Sub

Thank you in advance.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I am quite sure that I understand correctly, but maybe you can try by Worksheet_Change not Worksheet_SelectionChange, like below:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Application.CutCopyMode = 0 Then Exit Sub
If Application.CutCopyMode = xlCopy Then
    Target.Interior.Color = RGB(255, 255, 0)
End If
End Sub
 
Upvote 0
I am quite sure that I understand correctly, but maybe you can try by Worksheet_Change not Worksheet_SelectionChange, like below:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Application.CutCopyMode = 0 Then Exit Sub
If Application.CutCopyMode = xlCopy Then
    Target.Interior.Color = RGB(255, 255, 0)
End If
End Sub
Hi @KOKOSEK
Thank you for your response. The code you provided worked and highlighted the cells with a yellow color after I paste the values.
But, I would like the cell to change to yellow immediately after selecting it and pressing Ctrl+C.
 
Upvote 0
So change:

VBA Code:
Worksheet_Change

into
VBA Code:
Worksheet_SelectionChange
 
Upvote 0
We are almost there. Now, when you press Ctrl+C, nothing happens, and I have to click on another cell with the mouse to make them turn yellow.
 
Upvote 0
The issue is this:
- "Worksheet_Change" is fired when a value is manually changed in a cell
- "Worksheet_SelectionChange" is fired when a cell is selected

There is no built-in event to fire when CTRL+C, or copy, is selected.

I think you would have to get a little creative with your solutions. Check out some of suggestions here: VBA event trigger on copy?
 
Upvote 0
Solution
@Joe4

Thank you very much, I checked out some suggestions, and it worked very well, I used this code, and I assigned a shortcut key to my macro: Ctrl+C
VBA Code:
Sub CopyAndMarkAsCopied()
    Dim r As Range
    Set r = Selection
    With r
        .Copy
        .Font.Color = RGB(0, 0, 0) 'black font
        .Interior.Color = RGB(255, 255, 0) 'yellow background
        'whatever else
    End With
End Sub
 
Upvote 0
Excellent!
Glad to hear you got something that works for you!
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
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