Quantcast
Channel: Excel IT Pro Discussions forum
Viewing all articles
Browse latest Browse all 11829

Paste Values Only

$
0
0

Greetings All,

I need a code that will limit Excel 2010 to paste values only.  The code below works except that, on pasting, it strips the formatting out of the target cell, which is a deal breaker.  I've tried to modify the code that seems to be responsible (Target.ClearFormats) to no avail.  Any help is appreciated! 

Dim rngPrevious As Range    ' The previous range.
Dim rngNext     As Range    ' The next range.
Dim blnSwitch   As Boolean  ' If it is the first selection change.

' ########################
' Event for Workbook Open.
' ########################
Private Sub Workbook_Open()
    blnSwitch = False
    
    ' Record the active cell.
    Set rngPrevious = ActiveCell
End Sub

' ######################
' Event for SheetChange.
' ######################
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    ToggleEvents False
    If Application.CutCopyMode = xlCopy Then Target.Value = rngPrevious.Value
    If Application.CutCopyMode = xlCut Then Target.Value = rngPrevious.Value
    Target.ClearFormats
    ToggleEvents True
End Sub

' ###############################
' Event for SheetSelectionChange.
' ###############################
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Not blnSwitch Then
        Set rngNext = Target
        blnSwitch = True
    Else
        Set rngPrevious = rngNext
        Set rngNext = Target
    End If
End Sub

' ####################################
' Toggle the application-level events.
' ####################################
Private Sub ToggleEvents(ByVal Status As Boolean)
    Application.EnableEvents = Status
End Sub


Viewing all articles
Browse latest Browse all 11829

Trending Articles