Clearing the Clipboard in a Macro

Written by Allen Wyatt (last updated March 5, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


1

Jerry knows that, in Excel, there are several clipboards available. He wonders, though, if there is a way to clear each of the clipboards in a macro.

There are actually four different clipboards that you can tap into in Excel. The simplest one is the actual Excel clipboard, which is active anytime you see the marching ants around a selected range of cells. This clipboard can be cleared by using this single code line:

Application.CutCopyMode = False

The second clipboard is the Windows clipboard, which can be cleared using the following code, which works in 32-bit Excel environments:

Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function CloseClipboard Lib "user32" () As Long

Sub ClearClip()
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
End Sub

If you are using a 64-bit version of Excel (which means all Excel 2019 and Microsoft 365 versions), then you'll need to use code that is a bit different:

Declare PtrSafe Function OpenClipboard Lib "User32" (ByVal hwnd As LongPtr) As LongPtr
Declare PtrSafe Function EmptyClipboard Lib "User32" () As Long
Declare PtrSafe Function CloseClipboard Lib "User32" () As Long

Sub ClearClip()
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
End Sub

The difference between the 32-bit and 64-bit versions is the declaration lines, all of which are outside of the actual ClearClip subroutine.

The third and fourth clipboards are the Windows Clipboard History and the Office Clipboard, which are obviously Windows-level clipboards. Accessing them is more nitty-gritty and advanced than anything discussed so far. Of course, you may not care about clearing these Windows clipboards in Excel. Rather than explain it all here, you may appreciate this discussion at Stack Overflow:

https://stackoverflow.com/questions/64066265/clearing-the-clipboard-in-office-365

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12854) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Allowing for Words that Contain Numbers

When Excel does a spell-check of the information in a worksheet, you may want it to ignore words that contain numbers. ...

Discover More

WordTips Ribbon 2022 Archive (Table of Contents)

WordTips is a weekly newsletter that provides tips on how to best use Microsoft's word processing software. At ...

Discover More

Using a Filtered Value in a Formula

Accessing filtering criteria for use in a formula can be a real need for some worksheet designs. Getting to that ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Stepping Through a Non-Contiguous Range of Cells

Using macros to step through each cell in a selection is a common occurrence. What if that selected range is made up of ...

Discover More

Using Named Ranges in a Macro

Named ranges are a great capability provided by Excel. You can define all sorts of named ranges in a workbook, but how do ...

Discover More

Unprotecting Groups of Worksheets

Unprotecting a single worksheet is relatively easy. Unprotecting a whole lot of worksheets is harder. Here's how you can ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2022-03-05 11:19:05

J. Woolley

There is often confusion about Declare statements. VBA7 was introduced with Office 2010, the first to offer 32-bit and 64-bit versions. The Declare statements depend on your version of VBA, not your version of Excel (32-bit or 64-bit). If you have 32-bit or 64-bit Excel 2010 or later, use Declare PtrSafe.... In my humble opinion, anyone with an older version should upgrade.
The Tip's ClearClip macro applies to the legacy (single-item) Windows clipboard. There are two expanded (multi-item) clipboard features, each with its own user interface.
Office Clipboard is described here:
https://support.microsoft.com/search/results?query=Office+clipboard
Windows Clipboard History (Win+V, Settings > System > Clipboard) is described here:
https://support.microsoft.com/search/results?query=windows+clipboard+history
My Excel Toolbox includes the following three VBA7 macros:
ClearClipboard, which is like the Tip's ClearClip macro.
ClearOfficeClipboard, see https://stackoverflow.com/a/71326246/10172433
ClearClipboardHistory, see this abbreviated version:

Sub ClearClipboardHistory()
Const W = "wmic service where ""name like 'cbdhsvc[_]%'"" call "
Const C = "/c " & W & "stopservice" & " & " & W & "startservice"
CreateObject("Shell.Application").ShellExecute "cmd.exe", C, , "runas", 0
End Sub

See https://sites.google.com/view/MyExcelToolbox/


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.