Macro for Right Click Paste Option...

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Is there a macro when the user copies and then when the user right clicks to paste, which can grey out the following options:
Paste (P)
Formulas (F)
Transpose (T)
Formatting (R)
Paste Link (N)


But allow only the user to select:
Values (V)
On the menu options b/c it will not be greyed out???


Please let me know, as I’m anxious to test out some scripts.
Many thanks in advance.
R/ Pin-
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Code:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)

    Application.CommandBars("Cell").Controls(5).Enabled = False

End Sub

Note: This will disable the Paste Special option in the Excel Application on all worksheets and even other workbooks. You will need to re-enable it before you can Paste Special again. Also, this only affects the Cells menu, not the column, row or other menus. You can check out this webpage for details:

restrict spreadsheet to paste values only
 
Upvote 0
Hi mjbeam,

I did try out these codes and the ones on your links.

However, I can't seem to get it to work after pasting it into a module.

Do you know if you might have any more codes to offer?

Thank you!
 
Upvote 0
You don't want to paste it in a module, you need to add the middle line of code to the workshheet BeforeRightClick event.

There will be more to it that just that though. You need to have code in the appropriate events to enable and disable Paste Special as needed for your application. You will also need to disable/re-enable some of the other menu controls as I mentioned.
 
Last edited:
Upvote 0
Hi mjbeam,

I've got it and wanted to paste it!


Code:
Option Explicit
 
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
 
    '~~> 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
 
LetsContinue:
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.description
    Resume LetsContinue
End Sub


Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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