To clear the clipboard with VBA, the most reliable method is to call the Windows API function OpenClipboard, EmptyClipboard, and CloseClipboard from a module. Unlike the Application.CutCopyMode = False line, which only stops Excel's marching ants and does not empty the clipboard, the API approach actually removes the data so it cannot be pasted elsewhere. You need a Windows PC with Excel (or another Office app that supports VBA) and the ability to enable macros. The steps below use the 64-bit-safe LongPtr declarations so the code works in both 32-bit and 64-bit Office.
What You'll Need
- A Windows PC running Microsoft Excel (or another Office application that supports VBA). Mac Office does not support the Windows API calls used here.
- Macro security set to allow macros in the workbook you are using. You can enable macros per session via the yellow security bar, or set a Trusted Location in the Trust Center.
- A blank module in the Visual Basic Editor (Alt+F11, then Insert > Module).
- Basic familiarity with running a macro (Alt+F8 or assigning it to a button).
Step-by-Step
- Open the Visual Basic Editor. In Excel, press Alt+F11. The editor opens in a separate window. If the Project Explorer is not visible, press Ctrl+R.
- Insert a standard module. In the Project Explorer, right-click your workbook name, choose Insert, then Module. A new code window appears. Do not paste API declarations into a sheet module or ThisWorkbook module; they must live in a standard module.
- Add the API declarations at the top of the module. Paste the following exactly, including the
PtrSafekeyword and theLongPtrtypes, which keep the code compatible with both 32-bit and 64-bit Office:
The#If VBA7 Then Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hWndNewOwner As LongPtr) As Long Private Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long Private Declare PtrSafe Function CloseClipboard Lib "user32" () As Long #Else Private Declare Function OpenClipboard Lib "user32" (ByVal hWndNewOwner As Long) As Long Private Declare Function EmptyClipboard Lib "user32" () As Long Private Declare Function CloseClipboard Lib "user32" () As Long #End If#If VBA7block is what makes the code portable between old and new Office versions. If you omit it and use only the old declarations, 64-bit Office will raise a compile error. - Add the clearing subroutine below the declarations. Paste this procedure:
PassingPublic Sub ClearClipboard() Dim result As Long result = OpenClipboard(0&) If result = 0 Then MsgBox "Could not open the clipboard. Another program may be using it.", vbExclamation Exit Sub End If Call EmptyClipboard Call CloseClipboard End Sub0&as the owner handle means no window owns the clipboard, which is fine for a one-off clear. The error check matters: if another application has the clipboard locked,OpenClipboardreturns 0 and you must not callEmptyClipboardorCloseClipboardin that state. - Run the macro. Place the cursor inside the
ClearClipboardprocedure and press F5, or return to Excel and press Alt+F8, selectClearClipboard, and click Run. The clipboard is now empty; pasting anywhere will do nothing. - Test it. Copy some text from Notepad, run the macro, then try Ctrl+V in Notepad. Nothing should paste. If text still pastes, you are probably running a different macro or the declarations are in the wrong module.
- Call it from other code (optional). If you want the clipboard cleared automatically after a copy operation in your own macro, add
Call ClearClipboardafter yourCopyline. Note that this clears the entire clipboard, not just the range you copied. - Save the workbook as a macro-enabled file. Use File > Save As and choose
.xlsm. A standard.xlsxfile discards the VBA project when saved.
Common Mistakes
- Using
Application.CutCopyMode = Falseand expecting an empty clipboard. That line only removes the dashed border around copied cells in Excel. The data remains on the clipboard and can still be pasted into other programs. - Pasting API declarations into a sheet module. Declarations must be at the top of a standard module, before any procedures. Putting them in a worksheet or ThisWorkbook module causes a compile error.
- Omitting
PtrSafeandLongPtr. On 64-bit Office, old-style declarations trigger a compile error or, worse, crash the application at runtime. - Calling
EmptyClipboardwithout opening the clipboard first. The call fails silently and the clipboard is not cleared. - Forgetting
CloseClipboard. If you open the clipboard and do not close it, other applications may be unable to use the clipboard until Excel is closed. - Saving as
.xlsx. The macro disappears on save with no warning beyond a prompt you may have dismissed.
Tips
- Wrap the three API calls in a single procedure so you can reuse it from any macro with one line.
- If you only need to clear Excel's copy mode (the marching ants),
Application.CutCopyMode = Falseis faster and does not touch other applications' clipboard data. Use the API method only when you truly need the clipboard emptied. - Add a short delay or retry loop if you clear the clipboard immediately after a copy from another application; some programs hold the clipboard briefly.
- Keep the API declarations in a dedicated module named something like
modClipboardso they are easy to find and do not clutter your main code. - If you distribute the workbook, note that the code is Windows-only. Mac users will get a compile error on the
Declarestatements.
Troubleshooting
If the macro does nothing, first confirm you are running the right procedure: open the Immediate Window (Ctrl+G) in the editor and type ClearClipboard then press Enter. If you see a compile error, check that the declarations are at the very top of a standard module and that Option Explicit is not flagging an undeclared variable. If you get a "Can't find project or library" error, open Tools > References and look for a reference marked MISSING; uncheck it. If the clipboard still pastes old content, another application may be holding the clipboard open. Close that application, or wait a moment and run the macro again. If the error message "Could not open the clipboard" appears, the most common cause is a clipboard manager or remote-desktop session locking it; close the clipboard manager temporarily. If the macro works in one workbook but not another, the second workbook likely lacks the module or was saved as .xlsx. Finally, if you are on a Mac, the Windows API calls cannot work; there is no built-in VBA equivalent that reliably empties the system clipboard, so use a Windows machine for this task.
Frequently Asked Questions
Does Application.CutCopyMode = False clear the clipboard?
No. It only cancels Excel's copy or cut mode and removes the dashed border. The copied data stays on the Windows clipboard and can still be pasted into other programs.
Will this code work on a Mac?
No. The Declare statements call Windows system libraries that do not exist on macOS. The macro will fail to compile on a Mac.
Can I clear only part of the clipboard?
No. The Windows clipboard holds one item at a time (or a history managed by the operating system), and EmptyClipboard removes everything. There is no VBA method to remove a single entry.
Why do I get a compile error on the Declare line?
Usually because the declaration is in a sheet module or because the PtrSafe and LongPtr keywords were omitted for 64-bit Office. Move the declarations to a standard module and use the conditional block shown above.
Does clearing the clipboard affect clipboard history in Windows?
It empties the current clipboard content. Windows clipboard history, if enabled, may retain earlier items until they are cleared from the history itself; this VBA method does not manage that history.
Is there a built-in VBA command for this?
No. VBA has no native clipboard-clear method, which is why the Windows API calls are required. The three-call pattern shown here is the standard approach.