Paste ONLY paste the values, and nothing else

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
275
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2011
Platform
  1. Windows
  2. MacOS
In my workbook, I am trying to make it so if the user pastes any data from another cell in the workbook (or from another workbook), it will only paste the values from the original cell(s), but nothing else, such as the formatting, borders, cell background color, etc. I found some code in another forum for doing this:
https://stackoverflow.com/questions/34800859/excel-vba-always-paste-values-only

I put the code in the ThisWorkbook module, in the Workbook_SheetChange sub, because I want it to apply to ALL sheets of the workbook.

Here’s the code:

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
 
    Dim UndoList As String
 
    Application.ScreenUpdating = False
    Application.EnableEvents = False
 
    On Error GoTo Whoa
   
    If Application.CommandBars("Standard").Controls("&Undo").Enabled = True Then
 
        '~~> Get the undo List to capture the last action performed by user
        UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)
   
        '~~> Check if the last action was not a paste nor an autofill
        If Left(UndoList, 5) <> "Paste" And UndoList <> "Auto Fill" _
        Then GoTo LetsContinue
   
        '~~> Undo the paste that the user did but we are not clearing the
        '~~> clipboard so the copied data is still in memory
        Application.Undo
   
        If UndoList = "Auto Fill" Then Selection.Copy
   
        '~~> Do a pastespecial to preserve formats
        On Error Resume Next
        '~~> Handle text data copied from a website
        Target.Select
        ActiveSheet.PasteSpecial Format:="Text", Link:=False, _
        DisplayAsIcon:=False
   
        Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
        On Error GoTo 0
   
        '~~> Retain selection of the pasted data
        Union(Target, Selection).Select
   
    End If
 
LetsContinue:
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    On Error GoTo 0
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
 
End Sub

The code works exactly as it should if I try to copy & paste data within the same worksheet. BUT, if I copy a cell on one worksheet, and then paste it onto the cell in a different worksheet (inside the same workbook), it does not paste only the values. It also pastes the number formatting, background color, etc.

How can I make it so no matter what sheet in my workbook I am on, the paste function will only ever paste the value, and nothing else?
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Re: Need help making Paste ONLY paste the values, and nothing else

Does this work for you?
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim V As Variant
V = Target.Value
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Undo
    Target.Value = V
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub
 
Upvote 0
Re: Need help making Paste ONLY paste the values, and nothing else

Thanks for your suggestion.

This seems to have an effect on whenever I input data into the sheet that is not relating to doing a paste. For instance, If I type data into cell C30 and then press enter, normally the cell that is then selected is C31. However, with your code in the ThisWorkbook module, after I press enter in cell C30, the cursor highlights cell C31 for a moment, but then goes back up to C30. I don't want it to do that.

Also, with this code, I cannot paste data from a different workbook into my workbook. There may be times when my users need to copy data out of a different workbook into this one, and that is not working now. I can do a copy from another workbook, but when I attempt to paste it into my main workbook, nothing happens except for the "ding" sound that Excel sometimes makes.
 
Last edited:
Upvote 0
Re: Need help making Paste ONLY paste the values, and nothing else

Thanks for your suggestion.

This seems to have an effect on whenever I input data into the sheet that is not relating to doing a paste. For instance, If I type data into cell C30 and then press enter, normally the cell that is then selected is C31. However, with your code in the ThisWorkbook module, after I press enter in cell C30, the cursor highlights cell C31 for a moment, but then goes back up to C30. I don't want it to do that.

Also, with this code, I cannot paste data from a different workbook into my workbook. There may be times when my users need to copy data out of a different workbook into this one, and that is not working now. I can do a copy from another workbook, but when I attempt to paste it into my main workbook, nothing happens except for the "ding" sound that Excel sometimes makes.
This modification should remove the issue in bold font. As far as copy/paste from another workbook, I can't reproduce the issue you cite. Works fine for me.
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim V As Variant
V = Target.Value
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Undo
    Target.Value = V
    ActiveCell.Offset(1, 0).Select
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub
 
Upvote 0
Re: Need help making Paste ONLY paste the values, and nothing else

Thanks. Is there a way to make this code run only if the user is doing a PASTE?
 
Upvote 0
Re: Need help making Paste ONLY paste the values, and nothing else

Thanks. Is there a way to make this code run only if the user is doing a PASTE?
You are welcome. Replace earlier version with this:
Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim V As Variant
If Application.CutCopyMode = False Then Exit Sub
V = Target.Value
With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Undo
    Target.Value = V
    ActiveCell.Offset(1, 0).Select
    .EnableEvents = True
    .ScreenUpdating = True
End With
End Sub
 
Upvote 0
Re: Need help making Paste ONLY paste the values, and nothing else

is there not a way to inform said user to right click and paste as value instead of ctrl + v?
that would probably be the easiest solution, is to just paste as values.
sometimes you just have to beat it into end users.
 
Upvote 0

Forum statistics

Threads
1,214,535
Messages
6,120,090
Members
448,944
Latest member
sharmarick

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