How to clear the clipboard when certain cells are selected? (VBA)

Rnkhch

Well-known Member
Joined
Apr 28, 2018
Messages
528
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have some cells that I would prefer my students to type into them rather than just quickly copy/paste. I was wondering if a code could be written for this purpose?

In particular, I want to apply this to the range M4:O4 of my sheet to force my students to think and manually type in the required contents. So if they have copied something either in Excel or outside, I want the clipboard to immediately clear upon clicking on any of the cells in M4:O4.

Thanks much for any input!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
1. Put this code in a standard module:
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

Sub ClipboardClear()
  Call ClearClipboard
End Sub
2. Put this code in the worksheet that has the student input range you want to restrict from pasting:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("M4:O4")) Is Nothing Then
    Call ClipboardClear
    MsgBox "Cells M4:O4 require manual input, no pasting permitted"
    Exit Sub
End If
End Sub
The selection_change event macro will trigger automatically and clear the clipboard whenever one or more cells in the restricted range is selected.
 
Upvote 0
Thank you! I got this error:

"The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them the PtrSafe attribute."

Any tweaks can be done?
 
Upvote 0
Replace the standard module with this:
VBA Code:
Option Explicit
#If VBA7 Then
Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As LongPtr) As Long
Public Declare PtrSafe Function EmptyClipboard Lib "user32" () As Long
Public Declare PtrSafe Function CloseClipboard Lib "user32" () As Long
#Else
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
#End If

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

Sub ClipboardClear()
  Call ClearClipboard
End Sub
 
Upvote 0
Solution
Thank you! Now it works very well, but I encountered a small, unexpected issue that I hope you can fix for me:

I'm using the code from the solution post of my other thread (How to temporarily unprotect a sheet to allow VBA code to run and reprotect immediately, using VBA?) in the same sheet, and that code also contains a sub with the name "Worksheet_SelectionChange". So I get the error "Ambiguous name detected".

I tried changing the sub name in your code, but it didn't work.

Thank you so much!
 
Upvote 0
@Rnkhch, add the @JoeMo code for the worksheet_selection section
VBA Code:
If Not Intersect(Target, Range("M4:O4")) Is Nothing Then
    Call ClipboardClear
    MsgBox "Cells M4:O4 require manual input, no pasting permitted"
    Exit Sub
End If

to the top of your existing worksheet_selection section ;) In other words, combine the two sections to one section.
 
Upvote 0
Yay! Thanks! It's working like a charm 🧠 🍻 Another piece of the puzzle solved 🤗

Now which post should I mark as solution? 😅
 
Upvote 0
Cool, I marked post #4 as solution.

I think together with the other two threads we worked on, these must have been the most versatile/advanced 'copy/pasting' solution to date :cool: I'm super excited
 
Upvote 0
Glad I could help and happy to hear you've found lots of help from others here too.
 
Upvote 0

Forum statistics

Threads
1,215,398
Messages
6,124,694
Members
449,179
Latest member
kfhw720

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