Force Paste Special Values

hatman

Well-known Member
Joined
Apr 8, 2005
Messages
2,664
HAH... I think i have asked more questions today than in all the time since I became a member... I guess that means I'm pushing the envelope.

Anywho. I have a workbook where I want to retain FORMATS in all of my cells in all of my sheets, but allow the user to enter data. So I got this code to undo PASTE and instead do a PASTE SPECIAL VALUES:

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    Dim UndoString As String

    On Error GoTo err_handler

    UndoString = Application.CommandBars("Standard").Controls("&Undo").List(1)
    If Left(UndoString, 5) = "Paste" Then 'Only allow Paste Special|Values
            Application.ScreenUpdating = False
            Application.Undo
            Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                    :=False, Transpose:=False
            Application.ScreenUpdating = True
    End If

    Exit Sub

err_handler:

End Sub

Only I had a co-worker submarine this code by simply entering some data, then dragging to fill across. All my formats went south. Any way to fix that?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This looks extremely useful but I'm having difficulty pulling the undo control list from the command bars. The error handler kicks in whenever it reaches this line :

Code:
UndoString = Application.CommandBars("Standard").Controls("&Undo").List(1)

This is what I get from the debugger :

Method 'List' of object '_CommandBarComboBox' failed
<Method failed ?_CommandBarComboBox? object of ?List?><Method failed ?_CommandBarComboBox? object of ?List?><Method failed ?_CommandBarComboBox? object of ?List?><Method failed ?_CommandBarComboBox? object of ?List?>
Is there a library I need to include or does this need to be coded differently in different versions of Excel (I'm using XL2007...)

Thanks!

AOB</Method></Method></Method></Method>
 
Upvote 0
Hi,

I presume it is due to the fact that you are using Excel 2007. Since the command bars of the previous versions of Excel have been replaced by the ribbon, this is one of the things that are not "portable" to Excel 2007 and 2010.

You'll have to ask for help from some gurus in order to get a replacement script, my knowledge in that field has reached its limits ;-)

Rgds,

W.

This looks extremely useful but I'm having difficulty pulling the undo control list from the command bars. The error handler kicks in whenever it reaches this line :

Code:
UndoString = Application.CommandBars("Standard").Controls("&Undo").List(1)

This is what I get from the debugger :

Method 'List' of object '_CommandBarComboBox' failed
<Method failed ?_CommandBarComboBox? object of ?List?><Method failed ?_CommandBarComboBox? object of ?List?><Method failed ?_CommandBarComboBox? object of ?List?><Method failed ?_CommandBarComboBox? object of ?List?>
Is there a library I need to include or does this need to be coded differently in different versions of Excel (I'm using XL2007...)

Thanks!

AOB</Method></Method></Method></Method>
 
Upvote 0
Thanks Walky,

Actually, I think the issue is that the sheet in question is protected, which disables the undo function for some reason. I found by unprotecting the sheet first, it picked up the control list as expected!

Problem I have now is, for some reason it still seems to be pasting formats in some fashion, rather than pure values. I have conditional formatting set up on the sheet and when the user pastes in, the code appears to only paste values and my own CI kicks in. But oddly, if/when that pasted data is subsequently deleted, the format from the original workbook (i.e. from which that data was pasted) appears. Which is quite annoying...

But thanks for reverting! I'll continue to work away at it!

AOB
 
Upvote 0
This code is exactly what I've been looking for. Only one issue I can't seem to figure out-

When copying data from external sources, the undo/paste clears out the cell. For example, if I have a series of data in word or notepad and I paste it into a cell it will revert back to the data that was in the cell previously. Any ideas?
 
Upvote 0
Alternatively, you can replace

Code:
If UndoString = "Auto Fill" Then
                Set Srce = Selection
                Srce.SpecialCells(xlVisible).Copy
                Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False  
                Application.SendKeys "{ESC}"    
                Union(Target, Srce).Select      
                
            Else    ' PASTE
                Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End If

for

Code:
                If UndoString = "Auto Fill" Then
                Set Srce = Selection           
                Set Dest = Target
                
                Srce.AutoFill Destination:=Union(Srce, Dest), Type:=xlFillValues
                Application.SendKeys "{ESC}"   
                Union(Target, Srce).Select      
                
            Else    ' PASTE
                Target.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End If

So 'Auto fill' does the job instead of copying the same value, and also the clipboard is not longer used (no copy).
 
Upvote 0
This works well from worksheet to worksheet but not workbook to workbook nor from HTML.
For example, we have a system that users will copy a call tracking # from to the log (worksheet) which disables the formatting on the macro heavy workbook log. Any suggestions?
 
Upvote 0
I know this is a very old post (2006!), but I thought you should know I found this code to be exactly what I was looking for! Thanks very much.
 
Upvote 0
Just wondering if I'm doing something incorrect.. I'm very inexperienced with VBA.

We simply copy the code and paste it in the 'ThisWorkbook' file within the Microsoft Excel Objects folder..? Besides saving this & the workbook as a macro-enabled Excel, what am I missing?

Thanks in advance!
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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