Replace paste with pastespecial (user side only)

Gringoire

Board Regular
Joined
Nov 18, 2016
Messages
71
Office Version
  1. 365
Platform
  1. Windows
Hello guys,
To avoid importing undesired wrong external references in my sheet by a User, I found this snipped on the internet. It works correctly replacing a paste by a pastevalues.
The only issue is when I use an internal VBA procedure to programmatically paste some range :
1) it rise an error on Application.Undo line.
2) it prevent my Paste operation (that is in this case required by myself)

So, my question is: is there a way to detect if the paste operation comes from a User (unwanted) action or from a (wanted) code routine?

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Application.CutCopyMode = xlCopy Then
        Application.EnableEvents = False
        Application.Undo
        Target.PasteSpecial Paste:=xlPasteValues
        Application.EnableEvents = True
    End If
End Sub

many thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi, in your "internal procedure" you can disable events before the code makes changes to the worksheet with Application.EnableEvents = False and at the end of that procedure, you will need to re-enable events with Application.EnableEvents = True.
 
Upvote 0
Hmm.. it seems a good possibility. I'll give it a try even if I already have dozen of sheets and modules with tons of (sometime nested) procedures in my workbook, calling several paste or insert instructions and catch all of them could be a little tricky.

If there is no other way to separate internal paste calls from the User ones, I will use your workaround.

thank you.
 
Upvote 0
Yeah, this issue is related with a new feature that I would like to add to a five months work on vba code .
Unfortunately I realized too late the usefulness of that feature :(
I will try with this, hoping that it will not introduce new issues:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Application.CutCopyMode = xlCopy Then
        Application.EnableEvents = False
        On Error GoTo internalp
        Application.Undo
        Target.PasteSpecial Paste:=xlPasteValues
        Application.EnableEvents = True
    End If
internalp:
    Application.EnableEvents = True
    Exit Sub
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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