To block the usual copy-and-paste keyboard shortcuts in Excel for Microsoft 365 on Windows, use VBA’s Application.OnKey method to disable Ctrl+C and Ctrl+V while the workbook is active. This blocks those shortcuts, not every way to copy data, so it is a convenience or workflow control—not a security measure.
What the VBA code can and cannot do
Excel does not provide a workbook setting that guarantees users cannot copy its contents. VBA can intercept specified keystrokes while macros are running, but the workbook cannot reliably stop someone from using every Excel command, another application, or a screenshot. The distinction matters: blocking Ctrl+C and Ctrl+V can discourage accidental copying or standardize a workflow, but it cannot make displayed information confidential.
The code below also blocks Ctrl+X, Ctrl+Insert, and Shift+Insert, which are alternative keyboard shortcuts for cutting, copying, or pasting in Windows. It restores Excel’s default key behavior when the user switches away from the workbook or closes it. Because Application.OnKey acts at the Excel application level, the workbook must manage those shortcuts carefully rather than assuming the settings belong only to one worksheet.
Install the VBA code in Excel
Save a backup before running macros: VBA changes cannot be undone with Ctrl+Z. Save the working file as an Excel Macro-Enabled Workbook (.xlsm), then add the event procedures to ThisWorkbook and the helper procedure to a standard module.
- On Windows, press Alt+F11 to open the Visual Basic Editor. In the Project pane, find the workbook, then double-click ThisWorkbook.
- Paste the first code block into the ThisWorkbook code window. It runs when the workbook opens, becomes active, becomes inactive, and closes.
- In the Visual Basic Editor, choose Insert > Module. Paste the second code block into the new standard module.
- Save the workbook, close it, and reopen it with macros enabled. Test the shortcuts in a disposable copy before using the file for an important workflow.
Put this event code in ThisWorkbook:
Private Sub Workbook_Open()
' Block the selected shortcuts when this workbook opens.
SetCopyPasteKeys True
End Sub
Private Sub Workbook_Activate()
' Reapply the block when this workbook becomes active.
SetCopyPasteKeys True
End Sub
Private Sub Workbook_Deactivate()
' Restore Excel's default key behavior for other workbooks.
SetCopyPasteKeys False
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
' Restore keys before this workbook closes.
SetCopyPasteKeys False
End Sub
Put this helper procedure in a standard module:
Public Sub SetCopyPasteKeys(ByVal DisableKeys As Boolean)
' Disable or reset the selected Windows keyboard shortcuts.
If DisableKeys Then
Application.OnKey "^c", ""
Application.OnKey "^v", ""
Application.OnKey "^x", ""
Application.OnKey "^{INSERT}", ""
Application.OnKey "+{INSERT}", ""
Else
Application.OnKey "^c"
Application.OnKey "^v"
Application.OnKey "^x"
Application.OnKey "^{INSERT}"
Application.OnKey "+{INSERT}"
End If
End Sub
In the key strings, ^ represents Ctrl and + represents Shift. Passing an empty procedure string tells Excel to do nothing when that key combination is pressed. Calling Application.OnKey with the key but no procedure resets that key to its default Excel behavior.
How the workbook events manage the shortcuts
The helper does not run by itself; the workbook events call it. Workbook_Open applies the block at opening, while Workbook_Activate applies it again whenever the workbook becomes the active workbook. The activation event is useful if someone opens or switches to another workbook and later returns. Workbook_Deactivate restores the default key behavior when the protected workbook is no longer active, reducing the chance that the shortcut block affects unrelated work.
The close event is an additional cleanup step. Its Cancel argument is part of Excel’s event signature; the example does not change it. If another macro cancels closing, this procedure may already have reset the keys, so the block may need to be reapplied by activating the workbook again. Also, resetting a key this way restores Excel’s default behavior; it does not recover a custom shortcut mapping that another add-in or macro had assigned before this code ran.
Events depend on macros being allowed to run. If a user opens the file with macros disabled, or VBA events have been disabled by other code, these procedures may not run and the shortcut block will not be dependable. A workbook saved with the .xlsx extension cannot retain VBA code; use .xlsm for a macro-enabled workbook.
Shortcuts and commands that may still copy data
This code intercepts only the listed keys. It does not disable Excel’s Ribbon commands or context-menu commands. A user may still be able to select cells and choose Home > Copy or Home > Paste, right-click and select a command, drag cells, use the Excel menu, or copy data through another route. The precise options available depend on the selection, workbook state, and Excel version.
It is possible to write more VBA to disable certain menu items or right-click menus, but those controls are also limited and can affect Excel beyond the intended workbook. Menus and command bars can behave differently across versions and interfaces; a custom menu can be bypassed or restored, and preventing a user from copying one way does not prevent them from reading or re-entering visible values. Avoid presenting a shortcut block as a way to protect trade secrets, personal information, or regulated data.
There is another practical limitation: intercepting Ctrl+C can interfere with ordinary Excel work, including copying formulas, formatting, and ranges for legitimate purposes. Before distributing the workbook, tell users exactly which shortcuts are affected and provide an approved way to export or submit information if they need one.
Worksheet protection is not copy protection
Protecting a worksheet is useful when the goal is to prevent changes to formulas or layout. In Microsoft 365 for Windows, the typical path is Review > Protect Sheet. You can choose which actions are allowed, such as selecting locked or unlocked cells. However, sheet protection is not designed to stop someone from copying data they can see. If users can select and view a cell, they may still be able to copy its contents; protection settings should not be treated as a confidentiality boundary.
Workbook structure protection serves a different purpose: it can restrict changes such as adding, deleting, hiding, or moving sheets. It does not make the contents of visible sheets uncopyable. Choose protection based on the problem you actually need to solve: preventing accidental edits is different from limiting access to information.
Windows and Mac considerations
The examples target Excel for Microsoft 365 on Windows. The Windows shortcuts shown use Ctrl, including Ctrl+C and Ctrl+V. On a Mac, users commonly use Command+C and Command+V instead. Although Excel VBA is available on Mac, keyboard mapping and shortcut conventions differ; do not assume the Windows key strings in this example will block the corresponding Command shortcuts on every Mac setup. If you need a Mac-specific implementation, test the mappings in the exact Excel version and environment you support.
Excel for the web does not run VBA macros. Opening an .xlsm file in a browser therefore does not provide this VBA-based shortcut block. A user may also open a workbook in desktop Excel with macros disabled. If consistent behavior is essential, document the supported platform and macro requirements instead of assuming every Excel client enforces the same restriction.
Safer alternatives for common goals
For a workbook intended to prevent accidental edits, protect the sheet, unlock only the cells users should change, and test the resulting workflow. For a form-like task, provide clearly marked input cells and a separate output or submission process. These approaches guide users without implying that visible data cannot be copied.
For genuinely sensitive information, the stronger approach is to control who can access the data in the first place. Share only the fields and records each person needs, store the source in a system with appropriate access controls, and avoid distributing a full data file when a limited report will do. Encryption or permissions may reduce certain risks, but no control can prevent an authorized viewer from recording information they can see. Consult your organization’s security or compliance team where applicable.
Common problems and recovery
- The code has no effect: Check that the helper is in a standard module, the event procedures are in ThisWorkbook, the file is saved as
.xlsm, and macros are enabled. Confirm that application events have not been turned off elsewhere. - Shortcuts remain blocked after switching workbooks: Confirm that the deactivate event exists and that the workbook is actually deactivating. If event handling was disabled, the cleanup event may not have run.
- Copy works from the Ribbon: That is expected. The sample blocks listed keyboard shortcuts; it does not remove Ribbon commands or every other copy route.
- You need to restore shortcuts: Close the workbook normally so the close event can reset them, or run
SetCopyPasteKeys Falsefrom the VBA editor while the workbook is open. If other code has replaced Excel’s key assignments, resetting to defaults may not restore those custom assignments. - Excel behaves unexpectedly: Close and reopen Excel to clear application-level key assignments, then review the workbook’s event code. Keep a backup; macros cannot be reversed with Ctrl+Z.
Frequently Asked Questions
Can VBA completely prevent copying from an Excel workbook?
No. VBA can block selected shortcuts or commands while it runs, but it cannot reliably prevent all copying, screenshots, transcription, or access through other tools. Treat it as a workflow deterrent, not data security.
Does this code block Ctrl+X and the Insert-key shortcuts?
Yes. The sample blocks Ctrl+X, Ctrl+Insert, and Shift+Insert in addition to Ctrl+C and Ctrl+V. It does not block every possible copy, cut, or paste command in Excel.
Will worksheet protection stop users from copying cells?
Not reliably. Worksheet protection can limit editing and other sheet actions, but it is not a general feature for preventing users from copying visible data.
Does the code work in Excel for the web?
No. Excel for the web does not run VBA macros. The shortcut behavior in a browser cannot be enforced by this workbook code.
How do I turn copy and paste back on?
Close the workbook normally to run its cleanup event, or run SetCopyPasteKeys False from the VBA editor. Reopening Excel also clears application-level key assignments.
Will Ctrl+C be blocked on a Mac?
This example is written for Windows key combinations. Mac users generally use Command+C and Command+V, so test a Mac-specific mapping in the Excel version you support rather than relying on the Windows strings.
Related
- Microsoft Excel Shortcut Keys: Essential Commands
- Microsoft Excel Scenario Manager: Setup, Uses, and Limits
- Microsoft Excel: Remove Page Break Lines and Hide Them
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel UserForm: Change Font Size at Design Time or in VBATo change an Excel VBA UserForm’s font size, select the form or a control in the Visual Basic…
- VBA Loop Types: For, Do, and While ExplainedVBA has four commonly encountered loop constructs: For…Next, For Each…Next, Do…Loop, and While…Wend. Use For…Next when a counter…
- How to Create a Pivot Table Using VBA in Excel 365To create a PivotTable with VBA in Excel for Microsoft 365 on Windows, put your source data on…
- Excel VBA Range Syntax: Examples and Best PracticesIn Excel VBA, a range is a cell or group of cells, usually referenced with Range("A1") or Range("A1:C5").…
- Excel VBA List Worksheet Names: Methods and ExamplesTo list worksheet names with Excel VBA, loop through the workbook’s Worksheets collection and read each sheet’s Name…
- Excel VBA: How to Check Whether a Workbook Is OpenTo check whether a workbook is open in Excel VBA, loop through Application.Workbooks and compare each open workbook’s…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.