+ Reply to Thread
Results 1 to 8 of 8

VBA to enable copy/ paste as values OR formulas ONLY

  1. #1
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    VBA to enable copy/ paste as values OR formulas ONLY

    Hi all,

    I have a locked file in which clients put in the number of units they want to buy. Therefore, some of the cells must be unlocked for editing. The problem is, I use a lot of conditional formatting which gets messed up every time someone uses CTRL + C, CTRL + V as it copies not just the values but also the formats.

    Is there a way to have a macro disable everything BUT pasting as values OR formulas? Maybe whenever they press CTRL + C, CTRL + V, a message appears asking if data should be posted as values or formulas? Or any similar feasible solution to this??

  2. #2
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    280

    Re: VBA to enable copy/ paste as values OR formulas ONLY

    a heavy handed approach would be to re-build the conditional formatting using an event such as the sheet's change event.

    Please Login or Register  to view this content.
    Last edited by Gregor y; 06-22-2018 at 07:37 PM. Reason: fix bug, add error code
    If it'd make you feel better using my answer because of my street cred, then you can go ahead and click Add Reputation below to improve it.

  3. #3
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: VBA to enable copy/ paste as values OR formulas ONLY

    Thanks for sharing your sheet change event solution.

    Unfortunately, as you have already mentioned, this is a heavy handed approach, as there isn't only conditional formatting, but also number formats and colours that are affected. Do you know of any way that the original CTRL + C, CTRL + V shortcut could be "hijacked" with code to default it to pasting it as values? Do you know if this is generally speaking feasible?

  4. #4
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    280

    Re: VBA to enable copy/ paste as values OR formulas ONLY

    Not recommended as it disables CTRL+Z, which is why I did mine for CTRL+SHIFT+V

    Application.OnKey

    in ThisWorkbook
    Please Login or Register  to view this content.
    in a module
    Please Login or Register  to view this content.
    Note this solution also relies on knowing how the end user pastes their data, they could just as easily right-click and paste, SHIFT+INS paste...
    Last edited by Gregor y; 06-25-2018 at 05:58 PM. Reason: link cruft removal

  5. #5
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    280

    Re: VBA to enable copy/ paste as values OR formulas ONLY

    oh and since you're not doing it in your personal workbook you'd also want

    in ThisWorkbook
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    07-13-2017
    Location
    Hong Kong
    MS-Off Ver
    MS Office 365
    Posts
    481

    Re: VBA to enable copy/ paste as values OR formulas ONLY

    Quote Originally Posted by Gregor y View Post
    Public Sub Pval()
    Dim s As Range: Set s = Selection
    If Application.CutCopyMode = False Then GoTo PasteNorm
    Application.ScreenUpdating = False
    On Error GoTo PasteErr
    s.PasteSpecial Paste:=xlPasteValues, SkipBlanks:=True, Transpose:=False
    On Error GoTo 0
    Application.ScreenUpdating = True
    Exit Sub
    PasteNorm:
    On Error GoTo PasteNormErr
    s.PasteSpecial
    On Error GoTo 0
    s.WrapText = False
    EOFn: Application.ScreenUpdating = True
    Exit Sub
    PasteErr:
    Resume PasteNorm
    PasteNormErr:
    If Err = 1004 Then
    Beep
    On Error GoTo 0
    Resume EOFn
    End If
    On Error GoTo 0
    Resume
    End Sub
    Thanks Gregor! This is great, a big step in the right direction!

    Quote Originally Posted by Gregor y View Post
    Not recommended as it disables CTRL+Z, which is why I did mine for CTRL+SHIFT+V
    Is there no way to incorporate an "undo" function with code? Clients wouldn't be pleased if they suddenly can't undo accidental copy/pasting.
    I found the following article about such a function but my VBA skills aren't advanced enough to apply it to the above

    https://www.jkp-ads.com/Articles/UndoWithVBA00.asp

    Quote Originally Posted by Gregor y View Post
    Note this solution also relies on knowing how the end user pastes their data, they could just as easily right-click and paste, SHIFT+INS paste...
    It doesn't really matter if they could get around it by using right-click paste as etc.. I'm not trying to lock such features, just want to ensure that no one unknowingly messes up conditional formats as they aren't aware of the differences between pasting as values and CTRL+V. So they can still be able to paste formats/ formulas etc. but CTRL+C, CTRL+V should always default to "paste as values" as this is the shortcut 99% of people use.

  7. #7
    Forum Contributor Gregor y's Avatar
    Join Date
    10-24-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2010 32-Bit
    Posts
    280

    Re: VBA to enable copy/ paste as values OR formulas ONLY

    I haven't dug into the nitty-gritty of Application.OnUndo and Application.OnRepeat, but it doesn't look like the code you found would work for you since the actions it will Undo are actions performed by VBA not user actions; moreover the few poking around tests I did with OnUndo kinda worked, but it didn't preserve the stack so it'd fire the sub but you couldn't "undo" back into the user change history.

    my simplistic attempts:
    Please Login or Register  to view this content.
    I can put the question out there and see if anyone else has more experience with OnUndo, but since you "know" the formatting both conditional and otherwise that you want your sheet to have at the end of the day you're probably still better off using an event handler to rebuild/standardize it. Likely ThisWorkbook's Workbook_BeforeSave instead of the sheet's Worksheet_Change as I'd originally thought; since that should I think preserve all the undo stuff, not redirect any built in functionality, and be a little less heavy handed.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,460

    Re: VBA to enable copy/ paste as values OR formulas ONLY

    Responding to the question about undo: there is no way to preserve Excel's undo stack when VBA runs. It is possible to build your own undo handler from scratch to kind of take the place of Excel's undo stack/handler. See here: https://www.jkp-ads.com/Articles/UndoWithVBA00.asp
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. copy paste values except for formulas with =sum
    By ammartino44 in forum Excel General
    Replies: 4
    Last Post: 08-07-2015, 12:03 AM
  2. copy paste values for formulas
    By ammartino44 in forum Excel General
    Replies: 3
    Last Post: 05-07-2015, 10:03 PM
  3. Copy Formulas and Paste into the Last Set # of Rows Only & Paste Values
    By Mr. Ice in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-23-2014, 07:48 AM
  4. Copy paste only the format and formulas not values
    By sudharshan86 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-26-2014, 07:53 AM
  5. Macro Copy Paste values not formulas lines above 0
    By bast0504 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-06-2013, 01:30 PM
  6. Vba - find next empty column, insert column, copy paste values & copy paste formulas
    By DoodlesMama in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-20-2012, 12:43 PM
  7. Macro to Copy Paste Values and Formulas
    By thillis in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-19-2009, 11:55 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1