VBA & Macro Problems

How to Clear the Windows Clipboard Using VBA in Excel

Anum MushtaqAnum Mushtaq Nov 3, 2026 871 views

Question details

The user needs a VBA macro to empty the Windows system clipboard after copying large ranges of data in Excel.

Product
Excel
Device & OS
Windows
Scenario
Freeing up system memory or preventing accidental pasting by clearing the clipboard memory automatically after executing a heavy copy-paste macro.
Observed behavior
The clipboard retains large amounts of copied data, requiring a programmatic method using Windows API declarations within a standard VBA module to successfully empty it.
Before you start

Ensure that you are using a Windows-based system, as Windows API calls are not supported on Mac. Additionally, remember to save your workbook as a Macro-Enabled Workbook (.xlsm) so your VBA code is retained.

Solution 1Recommended

Use Windows API to Clear the Clipboard via VBA

Implement standard Windows API declarations in a standard VBA module to interact directly with the system clipboard and empty its contents.

To clear the system clipboard, VBA must call external functions from the 'user32' library. For modern 64-bit versions of Office, these declarations must include the 'PtrSafe' keyword to prevent compatibility errors.

It is crucial to place this code in a Standard Module rather than a worksheet module so that the macro is globally accessible and properly recognized by the Excel macro list.

1
Open the VBA Editor

Press Alt + F11 in Excel to open the Visual Basic for Applications (VBA) Editor window.

2
Insert a Standard Module

In the Project Explorer pane on the left side, right-click your workbook name, select 'Insert', and choose 'Module' from the context menu.

3
Paste the API Declarations and Macro

Copy and paste the following code into the new blank module window: Option Explicit Private Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As LongPtr Private Declare PtrSafe Function EmptyClipboard Lib "user32" () As LongPtr Private Declare PtrSafe Function CloseClipboard Lib "user32" () As LongPtr Public Sub ClearClipboard() OpenClipboard 0& EmptyClipboard CloseClipboard End Sub

4
Run the Macro

You can now run 'ClearClipboard' directly from the Macro dialog (by pressing Alt + F8) or call it at the end of your existing copy-paste macros to clear the clipboard memory.

Use Windows API to Clear the Clipboard via VBA
Macro Visibility: Using 'Public Sub' ensures the macro appears in Excel's drop-down macro list. If it does not appear, ensure the code is placed inside a Standard Module, not a Sheet or ThisWorkbook module.
Seamless VBA Compatibility

Run VBA Macros Smoothly with WPS Office

WPS Office Spreadsheet provides excellent support for VBA macros, allowing you to run complex automation tasks seamlessly, including Windows API calls. Experience high performance and robust compatibility with your existing Excel macros at no cost.

  1. 1. Download and Install WPS Office: Get WPS Office for free from the official website and install it on your Windows PC.
  2. 2. Open your Macro-Enabled Workbook: Launch WPS Spreadsheet and open your existing .xlsm file containing the clipboard clearing macro.
  3. 3. Enable Macros and Execute: Accept the security prompt to enable macros, press Alt + F8 to open the Macro list, and run the 'ClearClipboard' macro directly.
Fully compatible with Microsoft Excel macro files (.xlsm)Supports standard VBA modules and Windows API declarationsLightweight application that uses less system memoryFree to download with a highly familiar user interface
microsoft office alternative - wps office

Frequently Asked Questions

Why isn't my ClearClipboard macro showing up in the macro drop-down list?

If the macro does not appear, it is likely placed in a Sheet module or the ThisWorkbook module instead of a Standard Module. Ensure you right-click your project, select 'Insert', choose 'Module', and paste your code there.

Does this VBA clipboard code work on Mac?

No, this specific code calls the 'user32' library which is exclusive to the Windows API. It will not work on macOS. Mac users need to rely on AppleScript integrations invoked via VBA to clear the system clipboard.

Why is the data still in the clipboard after running the macro?

If data remains, ensure that no other clipboard manager (such as the Windows 10/11 Clipboard History feature triggered by Win+V) is actively holding the copied items. The macro clears the standard system clipboard, but specialized history managers might retain a copy.

What does the 'PtrSafe' keyword do in the macro code?

The 'PtrSafe' keyword is required for declaring API functions in 64-bit versions of Office. It explicitly tells the VBA compiler that the Declare statement is safe to execute in a 64-bit environment, preventing compilation errors.