Changing Excel's default paste mode to values only, Grabbing text from cell stored in object

CodePest

New Member
Joined
Jan 16, 2018
Messages
13
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.

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.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Ok, it appears the issue is related to the sheet being protected. However, I would assume that using Sheet.Unprotect would rectify the issue. Not sure why it works on an unprotected sheet, but not on a protected sheet.
 
Upvote 0
:)SOLUTION:

When the command Unprotect is used, excel will clear the clipboard. The simple solution was moving the unprotect line of my script to just after the clipboard text is stored in a string. When the sheet is unprotected, the clipboard is cleared but the text will remain in the string.

Here is the correct code for changing Excel's default paste mode to values only. Lastly, adding short cut key under macro options to CTL + v will make it so anything users copy and paste into your spreadsheet will not interrupt existing formats.

Code:
[FONT=Verdana]Sub PasteValuesOnly()[/FONT]

' Prevents screen flashing during hide/unhide, turns off screen updating
    Application.ScreenUpdating = False
    
' Establish Worksheet Names

    On Error GoTo OutofRange
    Dim Tracker As Worksheet
    Set Tracker = ActiveWorkbook.Worksheets("sheet1")
    
    Dim Sales As Worksheet
    Set Sales = ActiveWorkbook.Worksheets("sheet2")

    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

' Turn off protection
        Dim Pw1 As Integer
        Pw1 = 123
        Tracker.Unprotect Pw1
        Sales.Unprotect Pw1

    ' 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
    Exit Sub

OutofRange:
    If Err <> 0 Then MsgBox "Value out of range. Please close " & ThisWorkbook.Name & " to resume normal Cut/Copy/Paste mode."
     
[FONT=Verdana]End Sub[/FONT]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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