Consulting

Results 1 to 7 of 7

Thread: Solved: force pastespecial values

  1. #1
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location

    Solved: force pastespecial values

    Hi,

    Does anyone know of any code that will force pasting as values on a worksheet? There may be code out there but due to some restrictions on viewing forums at work (sure glad I can still get here) I can't view them.

    I want to force a paste to be pastespecial values for a worksheet no matter where the copy was done from and whether they use control + v, the edit menu or the context menu.

    Any ideas?
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    http://www.mrexcel.com/forum/excel-q...tml#post263183

    [VBA]
    Private Sub Worksheet_Change(ByVal Target As Excel.Range)

    If Application.CutCopyMode = xlCopy Then
    Application.EnableEvents = False
    Application.Undo
    Target.PasteSpecial Paste:=xlPasteValues
    Application.EnableEvents = True
    End If

    End Sub

    [/VBA]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    Thanks for that. I actually ended up going another way. I named a range that I wanted to protect and then used that in a script to capture the values that were pasted or changed and then enter them as values. I also wanted to protect any formulas in the range so added something to do that. I figured I would post it here as it may be useful to others. I actually think this would resolve peoples issues with validation being overwritten too.

    [VBA]Private Sub Worksheet_Change(ByVal Target As Range)
    Dim vValue As Variant
    If Not Intersect(Target, Me.Range("GIFormatted")) Is Nothing Then
    Application.EnableEvents = False
    vValue = Target.Formula
    Application.Undo
    If Target.HasFormula Then
    If Not Target.Formula = vValue Then
    MsgBox "Your last operation was canceled." & vbNewLine & _
    "It would have deleted an important formula."
    GoTo exiter
    End If
    End If
    Target.value = vValue

    End If

    exiter:
    Application.EnableEvents = True
    Exit Sub
    errorhandler:
    Application.EnableEvents = True
    MsgBox Err.Number & " - " & Err.Description
    End Sub
    [/VBA]
    -----------------------------------------
    The more you learn about something the more you know you have much to learn.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,644
    or ?

    [vba]Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Me.Range("GIFormatted")) Is Nothing Then
    on error resume next
    Application.EnableEvents = False
    for each cl in target.specialcells(-4123)
    cl.value=cl.value
    next
    application.enableevents=true
    end if
    End Sub[/vba]
    Last edited by snb; 02-08-2013 at 06:02 AM.

  6. #6
    VBAX Newbie
    Joined
    Jan 2014
    Posts
    1
    Location
    This will give you pastespecial instead of paste:

    Private Sub Worksheet_Change(ByVal target As Range)
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect
    Dim vCopyValue As Variant
    Dim vCopyFormula As Variant
    Dim CellAddr As String
    CellAddr = ActiveCell.Address
    On Error GoTo ExitClean
    vCopyValue = target.Value
    vCopyFormula = target.Formula
    Application.Undo
    target.Value = vCopyValue
    target.Formula = vCopyFormula
    On Error GoTo 0
    ExitClean:
    Application.CalculateFull
    Range(CellAddr).Select
    ActiveSheet.Protect Contents:=True, AllowFormattingRows:=True, AllowFormattingCells:=True, AllowInsertingHyperlinks:=True
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    End Sub

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,644
    @abline,

    Can you use code tags, please ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •