Hello,
I am attempting to make it so excel will only paste values when the command CTL + V is used. So, easy enough; I wrote this code and set the short cut key to CTL + V in the macro option window.
It works, I can copy text from the web and paste it into a cell and my formatting is not affected. Here is the problem
THE PROBLEM:
When I copy a cell and paste I get an error, unless I copy from the formula bar (which defeats the purpose of the code). For some reason, my code is not grabbing the text from the clipboard when copying a cell?
ERROR:
Run-time error '-2147221404(80040064)':
DataObject:GetText Invalid FORMATETC structure
Any help would be appreciated.
I am attempting to make it so excel will only paste values when the command CTL + V is used. So, easy enough; I wrote this code and set the short cut key to CTL + V in the macro option window.
Code:
Sub PasteValuesOnly()
' Prevents screen flashing during hide/unhide, turns off screen updating
Application.ScreenUpdating = False
' Establish Worksheet Names
Dim Tracker As Worksheet
Set Tracker = ActiveWorkbook.Worksheets("sheet1")
Dim Sales As Worksheet
Set Sales = ActiveWorkbook.Worksheets("sheet2")
' Turn off protection
Dim Pw1 As Integer
Pw1 = 123
Tracker.Unprotect Pw1
Sales.Unprotect Pw1
Dim myRange As Range
Set myRange = Selection
Debug.Print myRange.Address
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
On Error GoTo EmptyClip
'~~> Get data from the clipboard.
DataObj.GetFromClipboard
'~~> Get clipboard contents
myString = DataObj.GetText(1)
Debug.Print myString
' Copy string to selection
myRange = myString
myRange.Locked = False
' Prevents screen flashing during hide/unhide, turns off screen updating
Application.ScreenUpdating = False
Sales.Protect Pw1
Tracker.Protect Pw1
Exit Sub
EmptyClip:
If Err <> 0 Then MsgBox "Value not allowed."
Sales.Protect Pw1
Tracker.Protect Pw1
End Sub
It works, I can copy text from the web and paste it into a cell and my formatting is not affected. Here is the problem
THE PROBLEM:
When I copy a cell and paste I get an error, unless I copy from the formula bar (which defeats the purpose of the code). For some reason, my code is not grabbing the text from the clipboard when copying a cell?
ERROR:
Run-time error '-2147221404(80040064)':
DataObject:GetText Invalid FORMATETC structure
Any help would be appreciated.