Here, we have provided a dataset with clipboards.
Step 1: Insert a Module
- To add VBA code, select Visual Basic from the Developer.
- Select Insert >> Module to add VBA code.
Step 2: Copy the Code in the Module
- Enter the code below.
- Copy the code from here.
#If VBA7 Then
Private Declare PtrSafe Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, _
ByVal iChildStart As Long, ByVal cChildren As Long, _
ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Public Const myVBA7 As Long = 1
#Else
Private Declare Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, _
ByVal iChildStart As Long, ByVal cChildren As Long, _
ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Public Const myVBA7 As Long = 0
#End If
Public Sub EvRClearOfficeClipBoard()
Dim cmnB, IsVis As Boolean, j As Long, Arr As Variant
Arr = Array(4, 7, 2, 0) '4 and 2 for 32 bit, 7 and 0 for 64 bit
Set cmnB = Application.CommandBars("Office Clipboard")
With Application
.DisplayClipboardWindow = True
End With
IsVis = cmnB.Visible
If Not IsVis Then
cmnB.Visible = True
DoEvents
End If
For j = 1 To Arr(0 + myVBA7)
AccessibleChildren cmnB, Choose(j, 0, 3, 0, 3, 0, 3, 1), 1, cmnB, 1
Next
cmnB.accDoDefaultAction CLng(Arr(2 + myVBA7))
Application.CommandBars("Office Clipboard").Visible = IsVis
With Application
.DisplayClipboardWindow = True
End With
End Sub
Code Explanation This part is the conditional compilation section. It checks whether the VBA version is 7 (for 64-bit Excel) or not. It defines two functions AccessibleChildren, which interacts with the Windows Accessibility API (oleacc.dll), and a constant myVBA7, which holds a value depending on the VBA version (1 for 64-bit, 0 for 32-bit). Code Explanation Option Explicit: This statement enforces explicit declaration of all variables in the code, which helps avoid typos and ensures that all variables are declared before use. Public Declare PtrSafe Function…: These lines declare three Windows API functions, OpenClipboard, EmptyClipboard, and CloseClipboard, which interact with the clipboard. The PtrSafe keyword makes the declarations compatible with 64-bit versions of Excel. Public Function ClearClipboard(): This is a public function named ClearClipboard. Public functions can be called from other modules or procedures. OpenClipboard (0&): This line calls the OpenClipboard Windows API function to open the clipboard for access. The parameter (0&) indicates that the clipboard is opened for all windows (i.e., no specific window handle is provided). EmptyClipboard: This line calls the EmptyClipboard Windows API function to clear the clipboard’s contents. CloseClipboard: This line calls the CloseClipboard Windows API function to close the clipboard after the contents have been cleared. Sub ccc(): This is a Sub procedure named ccc. We use sub-procedures to execute a series of statements or actions. Call ClearClipboard: This line calls the ClearClipboard function we defined earlier. It clears the clipboard by opening, emptying, and then closing it.
This line sets the Application.CutCopyMode property to False, clearing any active clipboard operations and emptying the clipboard. This means that the clipboard will no longer contain copied or cut data after executing this line of code. Download Practice Workbook
Get FREE Advanced Excel Exercises with Solutions!
#If VBA7 Then
Private Declare PtrSafe Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, _
ByVal iChildStart As Long, ByVal cChildren As Long, _
ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Public Const myVBA7 As Long = 1
#Else
Private Declare Function AccessibleChildren Lib "oleacc" (ByVal paccContainer As Office.IAccessible, _
ByVal iChildStart As Long, ByVal cChildren As Long, _
ByRef rgvarChildren As Any, ByRef pcObtained As Long) As Long
Public Const myVBA7 As Long = 0
#End If
Public Sub EvRClearOfficeClipBoard()
Dim cmnB, IsVis As Boolean, j As Long, Arr As Variant
Arr = Array(4, 7, 2, 0) '4 and 2 for 32 bit, 7 and 0 for 64 bit
Set cmnB = Application.CommandBars("Office Clipboard")
With Application
.DisplayClipboardWindow = True
End With
IsVis = cmnB.Visible
If Not IsVis Then
cmnB.Visible = True
DoEvents
End If
For j = 1 To Arr(0 + myVBA7)
AccessibleChildren cmnB, Choose(j, 0, 3, 0, 3, 0, 3, 1), 1, cmnB, 1
Next
cmnB.accDoDefaultAction CLng(Arr(2 + myVBA7))
Application.CommandBars("Office Clipboard").Visible = IsVis
With Application
.DisplayClipboardWindow = True
End With
End Sub
Step 3: Run the Code
How to Disable Copy-Paste Without Clearing Clipboard in Excel
Option Explicit
Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As LongPtr
Public Declare PtrSafe Function EmptyClipboard Lib "user32" () As LongPtr
Public Declare PtrSafe Function CloseClipboard Lib "user32" () As LongPtr
Public Function ClearClipboard()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Function
Sub ccc()
Call ClearClipboard
End Sub
Option Explicit
Public Declare PtrSafe Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As LongPtr
Public Declare PtrSafe Function EmptyClipboard Lib "user32" () As LongPtr
Public Declare PtrSafe Function CloseClipboard Lib "user32" () As LongPtr
Public Function ClearClipboard()
OpenClipboard (0&)
EmptyClipboard
CloseClipboard
End Function
Sub ccc()
Call ClearClipboard
End Sub
Sub clear()
Application.CutCopyMode = False
End Sub
Things To Remember
VBA7 does not test x64 version of MSO, you need to test the Win64 for that reason.
Code is alright. Please inform your particular problem.