Clipboard error when copying shape

mcomp72

Active Member
Joined
Aug 14, 2016
Messages
308
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have VBA code in a workbook that copies some columns from a sheet and pastes them a few columns to the right on the same sheet. Then, another sub is called that makes a copy of a shape (text box) on that sheet. Everything was working great yesterday.

Today when I tried it, I received a run-time error.

Screenshot (355).png


In addition, there was a message in the bottom left of the Excel window.

Screenshot (354).png


If I clicked Don't show again, closed the Excel file, then tried again, the message would pop up again.

I have never seen the Clipboard error message before.

The first thing I tried was rebooting my computer. That didn't solve it. Still happened the next time I can the code.

I looked online and tried several things, but no luck.

Here's the sub that is trying to copy the shape. You'll see the various things I've tried.

VBA Code:
Sub CreateMainButton(SN As String, FirstPhaseID As Integer, NewID As Integer)

Dim PhaseName As String

With ThisWorkbook.Sheets(SN)
    
    'I have added these lines to try to avoid the clipboard error.
    .Activate
    Application.CutCopyMode = False
    .Range("A1").Copy
    Application.CutCopyMode = False
    Application.DisplayAlerts = False
    
    'This copies the button for Phase1.
    .Shapes("bt_main_Phase" & FirstPhaseID).Copy
    .Paste
    
    Application.DisplayAlerts = True
    
    Application.CutCopyMode = False

End With

End Sub

The error is happening on this line:
VBA Code:
.Shapes("bt_main_Phase" & FirstPhaseID).Copy

The columns & shape need to be copied on multiple sheets, so the code to copy the columns will run for one sheet, and then this sub will run for that same sheet. That process repeats multiple times via a For loop. The first time it runs (the sheet is named "A"), it works. It's on the second time around (the sheet is named "B") that it causes the error. I even had one time where it got to the third sheet (named "C") when the error occurred.

Again, this worked fine yesterday, so I don't have any idea what has suddenly happened to cause this. I'd appreciate any help, as I'm at a total loss.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You might need to clear out the clipboard on the OS.

While running Excel, you have two clipboards: Excel's and Windows.
You are already clearing Excel's clipboard with this...
VBA Code:
Application.CutCopyMode = False
but you are leaving Windows clipboard alone. This could be causing you issues if another program is accessing data from it.

To clear the clipboard completely you can use the following code:
(32 bit)
VBA Code:
Option Explicit

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

Public Function ClearClipboard()
  OpenClipboard (0&)
  EmptyClipboard
  CloseClipboard
End Function

(64 bit)
VBA Code:
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

Private Function ClearClipboard()
  OpenClipboard (0&)
  EmptyClipboard
  CloseClipboard
End Function

Test Call
VBA Code:
Sub TestClipboardClear()
  Call ClearClipboard
End Sub
 
Upvote 0
Thanks. I tried your suggestion, but unfortunately, the problem still occurred. After some more testing, I think I may have discovered the issue -- or at least part of it. When my code is running, there are two UserForms loaded and showing. (One UserForm is a "daughter" of the first one.)

If I pull out the relevant code within the UserForm modules and put it into a regular module, and thus run all the same code without the UserForms being loaded, the error does not occur, even if I don't run your code to clear the clipboard. So it seems that the fact that UserForms are loaded is causing this issue. Does that seem viable? I have never experienced anything like this before, so I really don't know. If so, would that seem like a bug in Excel? I am wondering if I should report this to Microsoft?

By the way, I'm running the following version of Excel on Windows 10.
Microsoft® Excel® for Microsoft 365 MSO (Version 2401 Build 16.0.17231.20236) 32-bit
 
Upvote 0
Update: I made a video showing the problem and sent it to Microsoft using the Feedback option in Excel. Someone responded and it does seem like this is a bug. They didn't indicate when or if it might be fixed, but at least they are aware of it and looking into it.
 
Upvote 0
I have made a couple of tools in work that have ran fine for years, now all of a sudden we are getting the clipboard issue... seems like this may be a bug due to a recent update.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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